Reformat array based on array value php - php

so I have an array similar to this
$arr[0]['name'] = 'Name';
$arr[0]['id'] = 2382;
$arr[1]['name'] = 'Name';
$arr[1]['id'] = 2838;
$arr[2]['name'] = 'Name';
$arr[2]['id'] = 2832;
How could I reformat the array replacing the initial index (0, 1, 2) with the id value of the array? Is this possible? The final array would be like this
$arr[1922]['name'] = 'Name';
$arr[2929]['name'] = 'Name';
$arr[3499]['name'] = 'Name';
Thanks

This is fairly straightforward.
It's simply a case of looping over the original array and building the new one as you go along.
Once you've finished you can, if you want, replace the new array with the old one.
foreach ( $arr as $thisArray ) {
$aNewArray[ $thisArray['id']]['name'] = $thisArray['name'];
}
$arr = $aNewArray;
If you have an arbitrary number of elements in the array and you just want to wipe out the ID and keep the rest you can unset the ID as you go along and use the resulting array:
foreach ( $arr as $thisArray ) {
$id = $thisArray['id'];
unset( $thisArray['id'] );
$aNewArray[ $id ] = $thisArray;
}
$arr = $aNewArray;

Related

storing multiple values in same array with same index

If I want to add multiple values in array having same index in PHP, then can it be possible to create this type of an array? For e.g.,
fruits[a]="apple";
fruits[a]="banana";
fruits[a]="cherry";
fruits[b]="pineapple";
fruits[b]="grappes";
I want array to look like as below:-
fruits = {[a]=>"apple",[a]=>"banana",[a]=>"cherry",[b]=>"pineapple",[b]=>"grappes"};
You cannot define multiple value under same key or index.
In your case -
fruits[a]="apple";
fruits[a]="banana";
Here apple will be replaced by banana.
Instead, you may define array as -
fruits[a][] = "apple";
fruits[a][] = "banana";
Edit: i updated my answer with php code, but i don't code php usually, this might not be the most optimal solution, i tried this code in a php sandbox
$subarray1[0] = "apple";
$subarray1[1] = "banana";
$subarray1[2] = "cherry";
$subarray2[0] = "pineapple";
$subarray2[1] = "grappes";
$fruits[0] = $subarray1;
$fruits[1] = $subarray2;
foreach( $fruits as $key => $value ){
foreach( $value as $key2 => $value2 ){
echo $key2."\t=>\t".$value2."\n";
}
}
use implode and explode .
subarray1[0] = "apple"
subarray1[1] = "banana"
subarray1[2] = "cherry"
subarray2[0] = "pineapple"
subarray2[1] = "grappes"
It is store data with ,(comma)
$ar="";
for($i=0;$i<=count(subarray1);$i++)
{
$ar[]=subarray1[$i];
}
$rt=implode(',',$ar);
echo $rt;
It is Remove ,(comma) form array
$ex=explode(",",$ar);
print_r($ex);

Pushing an Array element into an existing multidimensional array in codeigniter

I have a query in codeigniter like this
$query_tutors = $this->db->get_where("tutor_info", array('tutor_id' => $tutor_id));
I have also other array elements that I want to pass in the query which depends on some conditions.
So how do I push other multidimensional array elements to the existing array so I can pass the variable as a whole in the query?
array_push is not working in this case.
$array = array();
$array = array("tutor_id" => $tutor_id);
$array = array("online" => $online); // want to merge this to the 1st array.
$query_tutors = $this->db->get_where("tutor_info", $array);
First you're doing it wrong.
$array = array();
$array = array("tutor_id" => $tutor_id);
You're recreating the array again, which will delete it from the memory. Either you have to use
$array['tutor_id'] = $tutor_id;
$array["online"] = $online;
or
$array = array('tutor_id' => $tutor_id, 'online' => $online);
or if you want to merge two arrays
$array = array_merge(array('tutor_id' => $tutor_id), array('tutor_id' => $tutor_id));
Your initial code
$array = [];
$array = ["tutor_id" => $tutor_id];
Now, if you want to add conditional merge, simply follow,
if($condition)
{
$array = array_merge($array, ["online" => $online]);
}
If $condition == true You final array will be,
$array = ['tutor_id' => $tutor_id, 'online' => $online];
You are almost there, just need to read a little bit more about associative arrays.
Solution:
$array = array();
$array["tutor_id"] = $tutor_id;
$array["online"] = $online;
$query_tutors = $this->db->get_where("tutor_info", $array);
This way your $array will have all indexes which you want.
You can do something like this:
$array = array();
if (!empty($tutor_id))
{
$array["tutor_id"] = $tutor_id;
}
if (!empty($online))
{
$array["online"] = $online;
}
$query_tutors = $this->db->get_where("tutor_info", $array);

PHP in_array isn't finding a value that is there

I have an array called $friend_array. When I print_r($friend_array) it looks like this:
Array ( [0] => 3,2,5 )
I also have a variable called $uid that is being pulled from the url.
On the page I'm testing, $uid has a value of 3 so it is in the array.
However, the following is saying that it isn't there:
if(in_array($uid, $friend_array)){
$is_friend = true;
}else{
$is_friend = false;
This always returns false. I echo the $uid and it is 3. I print the array and 3 is there.
What am I doing wrong? Any help would be greatly appreciated!
Output of
Array ( [0] => 3,2,5 )
... would be produced if the array was created by something like this:
$friend_array = array();
array_push($friend_array, '3,2,5');
print_r($friend_array);
Based on your question, I don't think this is what you meant to do.
If you want to add three values into the first three indexes of the array, do the following:
$friend_array = array();
array_push($friend_array, '3');
array_push($friend_array, '2');
array_push($friend_array, '5');
or, as a shorthand for array_push():
$friend_array = array();
$friend_array[] = '3';
$friend_array[] = '2';
$friend_array[] = '5';
Array ( [0] => 3,2,5 ) means that the array element 0 is a string 3,2,5, so, before you do an is_array check for the $uid so you have to first break that string into an array using , as a separator and then check for$uid:
// $friend_array contains as its first element a string that
// you want to make into the "real" friend array:
$friend_array = explode(',', $friend_array[0]);
if(in_array($uid, $friend_array)){
$is_friend = true;
}else{
$is_friend = false;
}
Working example
Looks like your $friend_array is setup wrong. Each value of 3, 2, and 5 needs its own key in the array for in_array to work.
Example:
$friend_array[] = 3;
$friend_array[] = 2;
$firned_array[] = 5;
Your above if statement will then work correctly.

Whats the most elegant way to rearrange an associative array?

Suppose you have an associative array
$hash['Fruit'] = 'Apple';
$hash['Name'] = 'Jeff';
$hash['Car'] = 'Ford';
and you cannot change the order in which these variables are created. So Car is always added to the array after Name, etc. What's the prettiest way to add/move Car to the beginning of the associative array instead of the end (default)?
$hash = array('Car' => 'Ford') + $hash;
ksort() ?
But why would you care about the array's internal order?
array_reverse($hash, true);
This is not a very direct solution but one that is:
$value = end($hash);
$hash = array(key($hash) => $value) + $hash;
Another trick is -
$new_items = array('Car' => 'Ford');
$hash = array_merge($new_items, $hash);
You can re arrange the new array keys also. Suppose car first then another field (say Id) then array remain so....
$new_items = array('Car' => 'Ford','Id'=>'New Id');
$hash = array_merge($new_items, $hash);
The array will become like
$hash['Car'] = 'Ford';
$hash['Id'] = 'New Id';
$hash['Fruit'] = 'Apple';
$hash['Name'] = 'Jeff';

Change values in first key from 0 to count(array) - 1

Ok, I have an array like so:
$myArray[32]['value'] = 'value1';
$myArray[32]['type'] = 'type1';
$myArray[33]['value'] = 'value2';
$myArray[33]['type'] = 'type2';
$myArray[35]['value'] = 'value3';
$myArray[42]['value'] = 'value4';
$myArray[42]['type'] = 'type4';
Ok, looking for a quick way to change all numbers in the first key 32, 33, 35, and 42 into 0, 1, 2, and 3 instead. But I need to preserve the 2nd key and all of the values. The array is already ordered correctly, since I ordered it using a ksort, but now I need to reset the array from 0 - count($myArray) - 1 and keep the 2nd key intact and its value as well.
Can someone please help me?
$myArray = array_values($myArray);
There might be simpler solutions but here is one working solution:
$myArray = array();
$myArray[32]['value'] = 'value1';
$myArray[32]['type'] = 'type1';
$myArray[33]['value'] = 'value2';
$myArray[33]['type'] = 'type2';
$myArray[35]['value'] = 'value3';
$myArray[42]['value'] = 'value4';
$myArray[42]['type'] = 'type4';
$map = array_flip(array_keys($myArray)); // map old keys to new keys.
$newarray = array();
foreach($myArray as $key => $value) {
$newarray[$map[$key]] = $value; // use new key and old value.
}
You don't need it. Why not to just leave this array alone? Unnecessary moves would lead your code to a mess.

Categories