Get Key of "upper level" in array - php

I want to get the key of the array where, for example, "type" equals "UniqueType1" (in this case 0) in PHP.
The complete array is huge and from an API, so i can't modify the raw data.
The description of my problem is pretty bad but I've never done something similar. Sorry for that.
Array
(
[summary] => Array
(
[0] => Array
(
[type] => UniqueType1
[aggregated] => Array
(
....
)
[modifydate] => 1389890963000
)
[1] => Array
(
[type] => UniqueType2
[aggregated] => Array
(
....
)
[modifydate] => 1389890963000
)
) )

Unless I'm missing something, this looks like a case of simply iterating through an array and checking the value of a specific key in a sub-array.
Assuming that $array is your outer array...
foreach($array["summary"] as $index => $row)
{
if($row["type"] == "UniqueType1")
{
$targetIndex = $index;
break;
}
}
echo "The target index is " . (isset($targetIndex) ? $targetIndex : "not found.");

Related

Search part of array key on PHP

I have the following output when printing an array called yestardayArray using print_r:
Array
(
[project-id] => Array
(
[373482] => Array
(
[responsible-ids] => Array
(
[171812,129938] => Array
(
[0] => Array
(
[task-id] => 18055196
[content] => HU-002
[responsible-ids] => 171812,129938
)
)
[171812] => Array
(
[0] => Array
(
[task-id] => 18055300
[content] => HU-002
[responsible-ids] => 171812
)
[1] => Array
(
[task-id] => 18055307
[content] => HU-002 - BACK
[responsible-ids] => 171812
)
)
)
)
)
)
I'm iterating througth project-id (using the variable $pid), in the case of this example "373482", and also iterating througth responsible-ids with $key. As $key i'm using all the posible responsible-ids values for the project to get a match and do some stuff.
That work great in the case that there is only one responsible (because there is a full match), but if there are more, like in "171812,129938" there is no match.
How would you validate if $key (171812 or 129938) is part of responsible-ids ("171812,129938")?
I tried to convert the array key to a string, in order to use built-in php search functions like substr_count or strpos.
$needString = $yesterdayArray["project-id"][$pid]["responsible-ids"][$key];
But when I print needString I get "Array" instead of "171812,129938".
What can I do?
Call explode() on the keys, and then use in_array() to check if $key is in the array.
foreach ($yesterdayArray["project-id"] as $pid => $project) {
foreach ($project["responsible-ids"] as $resp_ids => $tasks) {
$resp_id_array = explode(',', $resp_id);
if (in_array($key, $resp_id_array)) {
// do something
}
}
}

Deleting complex array element

So I want to delete an array element from a JSON array based on an id in a sub-array. I know it sounds weird. Here's an example of the array. I want to delete the entire array [0] based on the [dealer][id] array where the [id] = 20220 in this example.
Array
(
[results] => Array
(
[offset] => 1
[length] => 15
[data] => Array
(
[0] => Array
(
[dealer] => Array
(
[id] => 20220
[name] => apple
)
)
)
)
}
In reality there are a lot more elements in the [results] array. I'm not sure how to go about it.
Any help is greatly appreciated!
Loop thru data key first then check if dealer id matches the searched id
$id = 20220;
foreach ($array['results']['data'] as $key => $value) {
if ($value['dealer']['id'] == $id) {
unset($array['results']['data'][$key]);
}
}
use array_filter,
$array['results']['data'] = array_filter($array['results']['data'], function($v){return $v['dealer']['id'] != 20220;});

Add a index key to existing array

I have a array, which I need to check if it's associative or not.
The array can look like this:
[preview] => Array
(
[0] => Array
(
[type] => web
[side] => left
)
[1] => Array
(
[type] => web
[side] => right
)
)
And that is perfect. But sometimes, I get this:
[preview_file] => Array
(
[type] => artwork
[side] => right
)
In this case, I need to add a index of 0 to make the array look like this:
[preview_file] => Array
(
[0] => Array
(
[type] => artwork
[side] => right
)
)
I'm using this function to check if it's assoctiative:
function is_assoc($array) {
return (bool)count(array_filter(array_keys($array), 'is_string'));
}
If not, I need to somehow add the [0]. Doe=s anyone have an idea?
I would check the values instead of the keys. It also sounds like you can get away just checking the first value:
function is_assoc($array) {
return is_array(reset($array));
}
Was your question about how to make the actual change? This ought to work:
if (!is_assoc($preview)) {
$preview = array($preview);
}
Different way:
if(!isset($array[0])) {
$array[0] = $array;
}

Multi-Dimensional array, returning wrong result

I have a multi-dimensional array (There is more than one item in "data" but i'm just showing one for this question):
Array
(
[data] => Array
(
[0] => Array
(
[to] => Array
(
[data] => Array
(
[0] => Array
(
[name] => fake name
[id] => 668071477234
)
[1] => Array
(
[name] => fake name
[id] => 1345556711
)
)
)
[updated_time] => 2012-12-24T23:46:26+0000
[id] => 327424994013537
)
)
)
I am trying to loop thru the array and determine if the id matches a variable sent from $_REQUEST, and if it does, I only want to return the "updated_time" value of the iteration.
Here's what I have but the date is always wrong, and doesn't match the proper iteration:
foreach($userOutbox['data'] as $outbox){
foreach($outbox['to']['data'] as $user){
if($user['id'] == $_REQUEST['facebook_id']){
$last_message_date = $outbox['updated_time'];
}
}
}
It's late and my eyes and brain are not helping me. Can anyone give me any direction?
Here's the solution that worked for me, just added break 2; Thanks for your help:
foreach($userOutbox['data'] as $outbox){
foreach($outbox['to']['data'] as $user){
if($user['id'] == $_REQUEST['facebook_id']){
$last_message_date = $outbox['updated_time'];
break 2;
}
}
}

array switch case statement

I have an array coming in with sub arrays like this
Array
(
[0] => Array
(
[customers] => Array
(
[id] =>
)
[Products] => Array
(
[id] =>
)
[Models] => Array
(
[id] => 151
[SubModels] => Array
(
[ol] =>
)
[Noice] =>
)
)
I want to make a switch statement on the array
so something like this
switch($array){
case Products:
case customers:
case Models:
}
how would I do that.
Thanks
since $array holds an array within it, it looks like you'll actually want to look at the keys of the array indexed at $array[0]
foreach ($array[0] as $key => $value) {
switch ($key) {
case 'Products' :
// do something
break ;
case 'customers' :
// do something
break ;
case 'Models' :
// do something
break ;
}
}

Categories