I have to access to all values stored in a big multidimensional array, here's an example of print_r the array:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
[Periodo] => Array
(
[#attributes] => Array
(
[FechaIngreso] => 1998-12-12
[FechaRetiro] => 2021-11-12
[FechaLiquidacionInicio] => 2021-05-01
[FechaLiquidacionFin] => 2021-05-30
[TiempoLaborado] => 10829
[FechaGen] => 2021-05-05
)
)
[Devengados] => Array
(
[Basico] => Array
(
[#attributes] => Array
(
[DiasTrabajados] => 30
[SueldoTrabajado] => 1258955.00
)
)
)
)
The thing I want to do is extract the values from that array, I have tried this way:
<?php
$cunenov = $array['Novedad']['#attributes']['CUNENov'];
but doesn't work..
Any suggests?. Thanks in advance.
I wanted you to see the "constructed" array and the outputs .. Stand alone, this php works .. Paste it into a stand alone php file and determine what you're doing differently to not achieve the same result.
Building out the array, and then print_r so you can validate it's the same structure as your array.
<?php
$test = Array();
$test['Novedad'] = array();
$test['Novedad']['#attributes'] = array();
$test['Novedad']['#attributes']['CUNENov'] = 4545454545;
print_r( $test );
Yields:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
)
Then we echo:
$cunenov = $test['Novedad']['#attributes']['CUNENov'];
echo "Value is $cunenov";
Yields
Value is 4545454545
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
}
}
}
I have a JSON Array
[0] => Array
(
[stage_id] => 80
[yieldVal] => Array
(
[0] => Array
(
[datajson] => [{"name":"doi","value":"215"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]
)
[1] => Array
(
[datajson] => [{"name":"doi","value":"698"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]
)
)
)
I need to extract the values from this Array
[0] => Array
(
[stage_id] => 80
[yieldVal] => Array
(
[doi_value] => 215
[doi_value] => 698
)
)
I have tried decoding the JSON. But unable to continue further.
$phpArray = json_decode($res['datajson'], true);
How to extract the values and assign the key.
EDIT : My final output should be
[0] => Array
(
[stage_id] => 80
[yieldVal] => 913 //215+698 -> Extracting values from [datajson]
)
One thing that may of tripped you up is that your datajson string is:
`[{"name":"doi","value":"215"},{"name":"dateofpollinationstops","value":"Date of Pollination Stops~23-3-2015"}]`
The square brackets mean that json_decode will create an array from the objects.
Anyway, try this...should give you the exact output you asked for:
$yieldVal = 0;
foreach ($res['yieldVal'] as $key => $arr) {
$decode = json_decode($arr['datajson']);
$yieldVal = $yieldVal + $decode[0]->value;
}
$newArray = array (
'stage_id' => $res['stage_id'],
'yieldVal' => $yieldVal
);
//var_dump($newArray);
echo "<pre>".print_r($newArray, true)."</pre>";
You should be able to get the value with:
$doi_value = $phpArray[0]['value'];
You can then sum them, push them onto a resulting array, or whatever.
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.");
I need some help. I have a variable containing this string;
[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]
I need to retrieve the id and value for each pair and make it any array in PHP.
You can use json_decode and pass the second param as true so it returns an array like this
$json = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$decoded = json_decode($json,true);
print_r($decoded);
Working Example
Output would be
Array
(
[0] => Array
(
[id] => 17
[value] => 123456789
)
[1] => Array
(
[id] => 18
[value] => 2012-06-13
)
[2] => Array
(
[id] => 19
[value] => Kampala
)
[3] => Array
(
[id] => 20
[value] => 1
)
.......
)
Which you can loop through using foreach like.
foreach($decoded as $de){
// access id with $de['id']
// access value with $de['value']
}
You have got an json string. You can convert it to an array by using function json_decode
Check this code .
$str = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"}, {"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$array = json_decode($str);
foreach($array as $temp){
echo "ID : ".$temp->id."\t Value: ".$temp->value;
echo "<br />";
}