I have a question for you, I need to through an array with other arrays in php but i through only the last array, my array is:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd+dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds+dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk+stw
)
)
My php:
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
With this code I through only the last array [2], but how to through array?please help me
I need to get the array:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk stw
)
)
$a_ddata = json_decode(method(), true); $i=0;
foreach ($a_ddata as $a_data)
{
$a_data_f[$i]['syn_id'] = $a_data['syn_id'];
$a_data_f[$i]['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
$i++;
}
This should be your answer..
When you iterate through something using foreach, by default PHP makes a copy of each element for you to use within the loop. So in your code,
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
// $a_data is a separate copy of one of the child arrays in $a_ddata
// this next line will modify the copy
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
// but at the end of the loop the copy is discarded and replaced with a new one
}
Fortunately the manual page for foreach gives us a way to override this behavior with the reference operator &. If you place it between the as keyword and your loop variable, you're able to update the source array within your loop.
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as &$a_data)
{
// $a_data is now a reference to one of the elements to $a_ddata
// so, this next line will update $a_ddata's individual records
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
// and you should now have the result you want in $a_ddata
This should help:
$a_data['syn_label'][] = urldecode(utf8_decode($a_data['syn_label']));
For each iteration you are only replacing $a_data['syn_label']. By adding [] you are making it a multi dimension array which increments for every iteration.
Related
Let's say I have an array like this:
[["code1": '528'], ["code2": '292'], ["code1": '108']]
I am looping through the array to check if a specific key exists. If it doesn't then I am adding it to another array:
$arr[$codename] = $code
However, if the key already exists, then I want to append the $code value to the existing key's values. I'm not sure how to do this part.
I want the new array to look like this:
[["code1": '528', '108'], ["code2", '292']]
You can try:
$array = [['code1' => 528], ['code2' => 292], ['code1' => 108]];
$newArray = [];
foreach ($array as $value) {
$newArray[array_key_first($value)][] = $value[array_key_first($value)];
}
print_r($newArray);
Result:
Array
(
[code1] => Array
(
[0] => 528
[1] => 108
)
[code2] => Array
(
[0] => 292
)
)
I have the following loop that creates an array
while($row1 = mysqli_fetch_assoc($result1))
{
$aliens[] = array(
'username'=> $row1['username'],
'personal_id'=> $row1['personal_id']
);
}
It produces the following result
Array (
[0] =>
Array ( [username] => nimmy [personal_id] => 21564865 )
[1] =>
Array ( [username] => aiswarya [personal_id] => 21564866 )
[2] =>
Array ( [username] => anna [personal_id] => 21564867 )
Then I have another loop as follows, inside which, I need to fetch the personal_id from the above array. I fetch it as follows.
foreach($aliens as $x=>$x_value)
{
echo $aliens['personal_id'];
//some big operations using the
$aliens['personal_id']; variable
}
However, I can't get the values if personal_ids. I get it as null. What seems to be the problem? How can I solve it?
You have an array of "aliens", each alien is an array with personal_id and username keys.
foreach ($aliens as $index => $alien) {
echo $alien['personal_id'], PHP_EOL;
}
The foreach loop iterates its items (aliens). The $alien variable represents the current iteration's item, i.e. the alien array.
foreach($aliens as $x=>$x_value)
{
echo $x_value['personal_id'];
//some big operations using the
$x_value['personal_id']; variable
}
I have the following code, as you can see I would like to create a new array inside the foreach. Even though its adding perfectly fine WITHIN the loop , all seems to be forgotten once the loop is finished.
foreach ($results as $result) {
$result['categories'] = array();
echo '<pre>';print_r($result);echo '</pre>';
}
echo '<pre>';print_r($results);echo '</pre>';
Result of first print_r
Array
(
[word_two_id] => 2
[categories] => Array
(
)
)
Array
(
[word_two_id] => 3
[categories] => Array
(
)
)
Array
(
[word_two_id] => 5
[categories] => Array
(
)
)
Array
(
[word_two_id] => 12
[categories] => Array
(
)
)
Result of second print_r
Array
(
[0] => Array
(
[word_two_id] => 2
)
[1] => Array
(
[word_two_id] => 3
)
[2] => Array
(
[word_two_id] => 5
)
[3] => Array
(
[word_two_id] => 12
)
)
$result in the foreach is going to be overwritten in your loop on every iteration. e.g. every time the loop rolls around, a NEW $result is created, destroying any modifications you'd done in the previous iteration.
You need to refer to the original array instead:
foreach ($results as $key => $result) {
^^^^^^^
$results[$key]['categories'] = array();
^^^^^^^
Note the modifications. You may be tempted to use something like
foreach($results as &$result)
^---
which would have worked, but also leave $result a reference pointing somewhere inside your $results array. Re-using $result for other purposes later on in the code would then be fiddling with your array, leading to very-hard-to-track bugs.
In PHP, the foreach loop operates on a shallow copy of the array, meaning that changes to the elements of the array won't propagate outside of that loop.
To pass the array elements by reference instead of by value, you put an ampersand (&) before the name of the element variable, like so:
foreach ($results as &$result) {
$result['categories'] = array();
echo '<pre>';print_r($result);echo '</pre>';
}
This way, any changes to the array elements are instead performed on a reference to that element in the original array.
Marc B made a good point in his answer regarding a consequence of using this method. After the foreach loop is done and the code continues, the variable $result will continue to exist as a reference to the last element in the array. So, you shouldn't reuse the $result variable without removing its reference first:
unset($result);
You will need the key too
try this
foreach ($results as $key=>$result) {
$result['categories'] = array();
$results[$key] = $result;
}
echo '<pre>';print_r($results);echo '</pre>';
Given
[0] => Array
(
[0] => ask.com
[1] => 2320476
)
[1] => Array
(
[0] => amazon.com
[1] => 1834593
)
[2] => Array
(
[0] => ask.com
[1] => 1127456
)
I need to remove duplicate values solely based on first value, regardless of what any other subsequent values may be. Notice [0][1] differs from [2][1] yet I consider this as a duplicate because there are two matching first values. The other data is irrelevant and shouldn't be considered in comparison.
Try this, assuming that $mainArray is the array you have.
$outputArray = array(); // The results will be loaded into this array.
$keysArray = array(); // The list of keys will be added here.
foreach ($mainArray as $innerArray) { // Iterate through your array.
if (!in_array($innerArray[0], $keysArray)) { // Check to see if this is a key that's already been used before.
$keysArray[] = $innerArray[0]; // If the key hasn't been used before, add it into the list of keys.
$outputArray[] = $innerArray; // Add the inner array into the output.
}
}
print_r($outputArray);
Hi this is my 2D array format. I want to remove 1st inside array.
Array
(
[0] => Array
(
[0] => Array
(
[type] => section-open
)
)
[1] => Array
(
[0] => Array
(
[type] => section-close
)
)
)
I want to remove all inside array and return it like this
Array
(
[0] => Array
(
[type] => section-open
)
[1] => Array
(
[type] => section-close
)
)
I tried array_shift function it's not working...
Update: This was based on the example the user gave, but he expected it to work for arrays with more than one element.
array_shift() removes the first element of an array, but that's not what you want.
You have to build something yourself.
Something like:
$result = array();
foreach($my_array as $element)
{
$result[]=$element[0];
}
Since you probably want a real 2d shift I made a function which does that, removing the first level in the array, but keeping ALL the items in the second level.
Here is a working example:
http://codepad.org/H7iaTI1E
And the function:
/**
* Removes first level in an array, returning the 2nd level elements as an array
* #param array Array to process
* #return 2nd level items from the given array
*/
function array2dshift(array $array) {
$res = array();
foreach($array as $lvl1) {
foreach($lvl1 as $item) {
$res[] = $item;
}
}
return $res;
}