I have an array (myArray) which looks like
Array(
[0] => Computer
[1] => House
[2] => Phone
)
I'm trying to set each value dynamically to a number for example
$newValues = [
"computer" => 0,
"House" => 1,
"Phone" => 2,
];
I have the below loop
$y = 0;
for ($x = 0; $x < count($myArray); x++){
$values = [
$myArray[$x] = ($y+1)
];
y++;
}
This incorrectly produces
Array(
[0] => 3
)
You can use array_flip($arr).
link
If I good understand, you want to flip values with keys, so try to use array_flip().
If becomes to work with array first try to do some research in PHP Array functions. ;)
use array_flip() which — Exchanges all keys with their associated values in an array
<?php
$a1=array("0"=>"Computer","1"=>"House","2"=>"Phone");
$result=array_flip($a1);
print_r($result);
?>
then output is:
Array
(
[Computer] => 0
[House] => 1
[Phone] => 2
)
for more information
http://php.net/manual/en/function.array-flip.php
Like the others have said, array_flip will work, however, your actual problems in the code you've written are:
You are using the wrong assignment operator for array keys:
$myArray[$x] = ($y+1) should be $myArray[$x] => ($y+1)
However this type of assignment really isn't necessary as the next problems will show:
You are overwriting $values each iteration with a new array.
To append to $values, you could use:
$values[$myArray[$x]] = $y+1;
If you really want 0 as your first value, don't use y+1 in your assignment.
Related
I'm trying to find when the array has 2nd dimension values that are the same so I can deal with them.
I've looked at array_unique and other people who are asking a similar question, but they all delete the values instead of returning them.
Say I have an array like this:
array(
[0] => array(
[laps] => 7,
[corrected_time] => 18
),
[1] => array(
[laps] => 6,
[corrected_time] => 18
),
[2] => array(
[laps] => 7,
[corrected_time] => 18.5
)
)
I'd like to have it return: array(0,1) because they both have the same value for corrected time
Here is one approach. First get the values for corrected_time and convert them to strings (because we'll use them in array_count_values, which only works on ints and strings).
$times = array_map('strval', array_column($your_array, 'corrected_time'));
Then find all the values that occur more than once using array_count_values and array_filter.
$repeats = array_filter(array_count_values($times), function($time) {
return $time > 1;
});
After you have this list of repeated times, you can use it to filter your original array to only include items with repeated times.
$multiples = array_filter($your_array, function($item) use ($repeats){
return isset($repeats[(string) $item['corrected_time']]);
});
You can iterate over this, or if you only want the keys, you can get them with
$keys = array_keys($multiples);
I have a function that returns an array where the value is an array like below: I want to ignore the key and extract the value directly. How can I do this without a for loop? The returned function only has one key but the key (2 in this case) can be a variable
Array ( [2] => Array ( [productID] => 1 [offerid]=>1)
Expected result:
Array ( [productID] => 1 [offerid]=>1)
There're at least 3 ways of doing this:
Use current function, but be sure that array pointer is in the beginning of your array:
$array = Array (2 => Array ( 'productID' => 1, 'offerid' => 1));
$cur = current($array);
var_dump($cur, $cur['offerid']);
Next is array_values function, which will give you array of values with numeric keys, starting with 0
$array = Array ( 2 => Array ( 'productID' => 1, 'offerid' => 1));
$av = array_values($array);
var_dump($av[0], $av[0]['offerid']);
And third option is use array_shift, this function will return first element of array, but be careful as it reduces the original array:
$array = Array ( 2 => Array ( 'productID' => 1, 'offerid' => 1));
$first = array_shift($array);
var_dump($first, $first['offerid']);
If you want to get the current value of an array, you can use current() assuming the array pointer is in the correct position. If there is only one value, then this should work fine.
http://php.net/manual/en/function.current.php
I think Devon's answer will work for you , but if not your can try
$arr = array_column($arr, $arr[2]);
if you need always the second index of your master array, if you need all index use
array_map(),
something like array_map('array_map', $arr); should work.
i want to restructure my array so that it looks better in a json
here is a print_r of my current variable:
Array
(
[0] => Array
(
[item_id] => 2
)
[1] => Array
(
[item_id] => 1
)
[2] => Array
(
[item_id] => 1
)
)
i want to reconstruct it be like this or similar:
EDIT
Array
(
[item_id] = array([0]=>'2',[1]=>'1', [2]=>'1');
)
sorry for my poor english m(_ _)m
i just want the item_id to have multiple values.
The hurdle
You actually can't in any way produce the output that you desire, since the key needs to be unique.
You can't use a key of item_id more than once, every time you try and set it, it will override what was in there last.
Think about it, how do you then look up the item with key of item_id, you can't, because three things would have that same key.
If the only reason is for cosmetics, I'd leave the output as you currently have it, although it may look a little messy in your JSON, it works.
A different approach
The best you can hope, is to get an output of:
'item_id' => array(
2,
1,
1
)
You can do this with the help of the array_map function:
$array = array('item_id' => array_map('current', $array));
This can be accomplished using this code.
$a['item_id'] = array();
foreach($arr as $key=>$val) {
$a['item_id'][] = $val['item_id'];
}
print_r($a);
$array = array('item_id' => array_map('current', $array));
I have an array in PHP that looks like
Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) )
I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.
Try this
array_slice($array, 0, 1);
http://php.net/array_slice
If you don't know the exact keys, you could do something like this:
$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];
array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here
http://codepad.org/YXu6884R
Here you go. See above for proof. The methodology from #azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.
$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );
echo $my_array['123654'][0];
Try
$first = array_shift(array_values($array));
http://php.net/manual/en/function.array-shift.php
Hey everyone, I have a database result that returns from a method. I need to push 4 more values onto the stack but I need to name the keys. array_push() automatically assigns an int. How can I overcome this behavior?
Array
(
[these] => df
[are] => df
[the] => sdf
[keys] => sd
[ineed] => daf
[0] => something
[1] => something
[2] => something
[3] => something
)
The keys that are int values need to be changed. How can I do this using array_push?
Just like this:
$arr['anotherKey'] = "something";
$arr['yetAnotherKey'] = "something";
$arr['andSoOn'] = "something";
or
$arr = array_merge($arr, array(
'anotherKey' => "something",
'yetAnotherKey' => "something",
'andSoOn' => "something"
));
...but I'd recommend the first method, since it merely adds more elements to the array, whereas the second will have a lot more overhead (though it's much more flexible in some situations).
If the four values you want to use are already in an associative array themselves, you can use + to merge the two arrays:
$array1 = array('these' => ..., 'are' => .., 'keys' => ...);
$four_entries = array('four' => ..., 'more' => ..., 'keys' => ..., '!' => ...);
$merged_array = $array1 + $four_entries;
Why not
$arr["whateveryouwant"] = something
Note: If you use array_push() to add
one element to the array it's better
to use $array[] = because in that way
there is no overhead of calling a
function.
If you want to add more entries to the array, all you need to do is:
Existing array;
$array =
{
"these" => "df"
"are" => "df"
"the" => "sdf"
"keys" => "sd"
"ineed" => "daf"
}
Adding to the array
$array["new_key1"] = "something";
$array["new_key2"] = "something";
If you want to assign the name you do not use the array_push function, you just assign the element:
$array['somekey'] = 'somevalue';
So, in short, you can't do that using array_push.