Reputation: 8597
I have decided to set up an array to differentiate certain values. If I look to find a certain value in the array, I'd like to use the rest of the data within this value I have searched:
Array
(
[0] => Array
(
[id] => 123
[0] => Array
(
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
)
[1] => Array
(
[id] => 617
[0] => Array
(
[name] => reeeJoint
[pluralName] => reeeJoints
[shortName] => reee
)
)
[2] => Array
(
[id] => 12355
[0] => Array
(
[name] => LeftJoint
[pluralName] => LeftJoints
[shortName] => Left
)
)
[4] => Array
(
[id] => 526
[0] => Array
(
[name] => asdfJoint
[pluralName] => asdfJoints
[shortName] => asdf
)
)
)
I'd like to do some kind of search where if there's a value with 123
, it will only get a new array with everything in the 123 id array (in this case is the first one).
What is the best practice in finding this?
Thanks!
Edit:
Based on some comments, I was able to come up with a new way of setting my array. However I'm still wondering the same question which way to tackle showing if value exists:
foreach($values as $value)
{
...
$categoriesExtract[$id] = array($category_stringArray2Sugg);
}
gives me this array:
Array
(
[123] => Array
(
[0] => Array
(
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
)
[617] => Array
(
[0] => Array
(
[name] => reeeJoint
[pluralName] => reeeJoints
[shortName] => reee
)
)
[12355] => Array
(
[0] => Array
(
[name] => LeftJoint
[pluralName] => LeftJoints
[shortName] => Left
)
)
[526] => Array
(
[0] => Array
(
[name] => asdfJoint
[pluralName] => asdfJoints
[shortName] => asdf
)
)
)
Upvotes: 0
Views: 244
Reputation: 8597
I think I just made something easy more complicated...
Because of the suggestions, I was just able to get the array by doing this
$array(123); => this will just get me the array that has the value 123...
Upvotes: 0
Reputation: 32760
Try this solution : Here change the values in $find
array to search for ids
$array = Array(Array("id" => 123,0 => Array("name" => "Burger Joint","pluralName" => "Burger Joints","shortName" => "Burgers")),
Array("id" => 617,0 => Array("name" => "reeeJoint","pluralName" => "reeeJoints","shortName" => "reee")),
Array("id" => 12355,0 => Array("name" => "LeftJoint","pluralName" => "LeftJoints","shortName" => "Left")),
Array("id" => 526,0 => Array("name" => "asdfJoint","pluralName" => "asdfJoints","shortName" => "asdf"))
);
$find = array(123,526); /// Get all the array with this id
$result = array();
foreach($array as $vals){
if(in_array($vals['id'],$find)){
$result[] = $vals;
}
}
echo "<pre>";
print_r($result);
Upvotes: 0
Reputation: 7776
You can simple do like this:
$array = array(0 =>
array('id' => 123,
0 => array('name' => 'Burger Joint',
'pluralName' => 'Burger Joints',
'shortName' => 'Burgers')
)
);
$new_array = array();
foreach($array as $value){
$new_array[$value['id']][] = $value[0];
}
Output will be
Array
(
[123] => Array
(
[0] => Array
(
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
)
)
Cheeers !!!!!
Check working demo here working demo link
Upvotes: 0
Reputation: 3536
Why don't you use the id as the key, then pull out the key?
$array = Array
(
[123] => Array
(
[name] => Burger Joint
[pluralName] => Burger Joints
[shortName] => Burgers
)
[617] => Array
(
[name] => reeeJoint
[pluralName] => reeeJoints
[shortName] => reee
)
)
$new_array = $array[123];
You had 2 items with the ID 123, which would mean this situation wouldn't work, but in your question you indicated you wanted the first value returned. Is the double up on the ID an error, or is it something that can legitimately happen because this will mean this won't be a suitable way of doing it.
Upvotes: 1
Reputation: 30488
Use this code. It will create new array following the match.
$arr = array();
for($i = 0;$i<count($array);$i++)
{
if($array[$i][id] == 123)
{
$arr[] = $array[$i][0];
}
}
print_r($arr);
Upvotes: 1