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
}
}
}
Related
I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
I have a multi-dimensional & nested array like below:
Array
(
[_edit_lock] => Array
(
[0] => 1504299434:6
)
[_edit_last] => Array
(
[0] => 6
)
[additional_photos_0_gallery_image_1] => Array
(
[0] => 77556
)
[additional_photos_0_gallery_image_2] => Array
(
[0] => 77567
)
[additional_photos_0_gallery_image_3] => Array
(
[0] => 73768
)
....
)
Now I need to get only those elements of the given array(in a separate array without changing current array), whose keys match a particular pattern like below:
additional_photos_[any_number]_gallery_image_[any_number]
How can I get using one of the array functions and avoiding foreach loops ?
Just use array_filter and preg_match.
return array_filter($data, function($key) {
return preg_match('~^additional_photos_[0-9]+_gallery_image_[0-9]+$~', $key);
}, ARRAY_FILTER_USE_KEY);
I've got an array, called $data which needs to be updated with data from an ajax call.
There are two variables sent via an ajax call (with example inputs):
sectionDetails:
[111][0][2][0][service_providers][]
serviceProvider:
Google
The serviceProvider is the data, and the sectionDetails is the array in which the serviceProvider should be in, in the $data array.
What I need is the $data array to end up as:
$data = Array
(
[111] => Array
(
[0] => Array
(
[2] => Array
(
[0] => Array
(
[service_providers] => Array
(
[0] = Google
)
)
)
)
)
)
This way, I can dynamically input data into any cell and then later I can update specific arrays (e.g. $data[111][0][2][0][service_providers][0] = "Yahoo";
The $_POST['sectionDetails'] is however a string which is where the issue is.
Is there a way to change this string to an array that can then be appended to the main $data array (and in the case of an existing value in the same section, update the value)?
Hope that makes sense.
If you create a function like this:
function setToPath(&$data, $path, $value){
//$temp will take us deeper into the nested path
$temp = &$data;
//notice this preg_split logic is specific to your path syntax
$exploded = preg_split("/\]\[/", rtrim(ltrim($path,"["),"]"));
// Where $path = '[111][0][2][0][service_providers][]';
// $exploded =
// Array
// (
// [0] => 111
// [1] => 0
// [2] => 2
// [3] => 0
// [4] => service_providers
// [5] =>
// )
foreach($exploded as $key) {
if ($key != null) {
$temp = &$temp[$key];
} else if(!is_array($temp)) {
//if there's no key, i.e. '[]' create a new array
$temp = array();
}
}
//if the last index was '[]', this means push into the array
if($key == null) {
array_push($temp,$value);
} else {
$temp = $value;
}
unset($temp);
}
You can use it like this:
setToPath($data, $_POST['sectionDetails'], $_POST['serviceProvider']);
print_r($data) will return:
Array
(
[111] => Array
(
[0] => Array
(
[2] => Array
(
[0] => Array
(
[service_providers] => Array
(
[0] => Google
)
)
)
)
)
)
Being very careful and depending on the situation, you could use eval:
eval("\$data$sectionDetails = '$serviceProvider';");
print_r($data) will return:
Array
(
[111] => Array
(
[0] => Array
(
[2] => Array
(
[0] => Array
(
[service_providers] => Array
(
[0] => Google
)
)
)
)
)
)
Caution
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
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.
In PHP, Codeigniter: my array, $phoneList, contains the following:
Array
(
[name] => One, Are
[telephonenumber] => 555.222.1111
)
Array
(
[name] => Two, Are
[telephonenumber] => 555.222.2222
)
Array
(
[name] => Three, Are
[telephonenumber] => 555.222.3333
)
How do I list each name out? Each number out? Am I right in saying my array contains three different Arrays? And is that normal, for an array to contain arrays?
When I do a print_r($phoneList), I get the following:
Array ( [0] => Array ( [name] => One, Are [telephonenumber] => 555.222.1111 ) [1] => Array ( [name] => Two, Are [telephonenumber] => 555.222.2222 ) [2] => Array ( [name] => Three, Are [telephonenumber] => 555.222.3333 ) )
You'll probably want to use foreach to loop through them. Something like this:
foreach($data as $arr) { // assuming $data is the variable that has all this in
echo $arr['name'].": ".$arr['telephonenumber']."<br />";
}
Here is the solution. Foreach is the easiest approach.
It's completely normal to have an array of arrays (in this case an array of associative arrays). They can be written like so:
$arrayofarray = array(array('name' => 'aname', 'phone'=>'22233344444'), array('name' => 'bobble', 'phone'=>'5552223333'));
print_r($arrayofarray);
and you should be able to print out the content in this way:
foreach ($arrayofarray as $arr){
print $arr['name']."\n";
print $arr['phone']."\n";
}
If you want to know what terms are set in each associative array you can use array_keys() to return them (as a simple array). For example:
foreach ($arrayofarray as $arr){
$setterms=array_keys($arr);
foreach ($setterms as $aterm){
print "$aterm -> ".$arr[$aterm]."\n";
}
}