I want to change the number in the first array in a multidimensional array. I have a code that outputs the value to an array and there is no chance for it to start counting from one - in my code. So my idea is to change the value starting from one - after it has been declared. My array look like this:
Array
(
[53] => Array
(
[name] => Volkswagen
[regularePrice] => 2139.00
)
[54] => Array
(
[name] => BMW
[regularePrice] => 2219.00
)
[55] => Array
(
[name] => Chrysler
[regularePrice] => 2399.00
)
)
I want - through a while or for - go through the array and change the values 53 to 1, 54 to 2, 55 to 3 and so on depending on how long the array is.
How do I accomplish this?
The answer is:
array_values($arr);
did you try:
$array = array_values($array);
Related
This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed last month.
I have an array with multiple key.
Array 1
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Output
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Expected Output
$age = array("value"=>"35", "value"=>"37", "value"=>"43");
Array ( [value] => 35 [value] => 37 [value] => 43 )
You can't
Indeed, array keys must be unique. Otherwise, what should the program output when you try to access value ?
But ...
If you need to store a list of value for one key, you can use array of arrays.
$array = array("value" => array());
array_push($array["value"], 35, 40, 53);
print_r($array)
And the output will be:
Array
(
[value] => Array
(
[0] => 35
[1] => 40
[2] => 53
)
)
Only way is to turn this array into 2D array:
$age = array(
array("value" => "35"),
array("value" => "37"),
array("value" => "43")
);
-- Output --
Array
(
[0] => Array
(
[value] => 35
)
[1] => Array
(
[value] => 37
)
[2] => Array
(
[value] => 43
)
)
-- Usage --
$age[0]['value'];
$age[1]['value'];
$age[2]['value'];
But it completely depends if $age array is in our control and can be changed.
You can't
Array keys must be unique.
http://php.net/manual/en/language.types.array.php
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Yes, array key should be unique. So, whatever you are asking it's not possible. Can you tell us what's requirements? So that, people can suggest any alternate solution.
This cannot be possible with native php arrays. What you need is a multimap, and you can find several implementations of it on github.
For example: link
EDIT: the link above is an interface. you need to include also link2
I'm trying to access a piece of data in an array of arrays that (I believe) is in an object (this may not be the right term though).
When I do print_r on this: $order_total_modules->process() I get...
Array (
[0] => Array (
[code] => ot_subtotal
[title] => Sub-Total:
[text] => $49.99
[value] => 49.99
[sort_order] => 1
)
[1] => Array (
[code] => ot_total
[title] => Total:
[text] => $0.00
[value] => 0
[sort_order] => 12
)
)
If I run echo $order_total_modules->process()[1][3];, I should get "0", because that is the 3rd element of the 2nd array... right? Yet, I get an error.
Can anyone help with this?
Even though it is the third element counting from 0, the index is not 3 it is an associative array with the index value:
Available in PHP >=5.4.0:
echo $order_total_modules->process()[1]['value'];
Or PHP < 5.4.0:
$result = $order_total_modules->process();
echo $result[1]['value'];
You cannot access an associative array via an integer index(unless the index is an actial integer).
So in this case use :
[1]['code'] to access what woulde be [1][0] with a 'normal' array.
Try putting it in a var first:
$ar = $order_total_modules->process();
echo $ar[1]['value'];
The second level array is an assoc, which means that the key is not numeric, which means that you need to call the name of the key, hence the 'value'.
I know this won't take much time for experts here. But still please help me out
My Array output is like this
Array ( [0] => 1 [1] => 37 [2] => 1035 ) 1
Array ( [0] => 1 [1] => 37 [2] => 1035 ) mystatusmessage1
Array ( [0] => 4 [1] => 37 [2] => 2925 ) 2
Array ( [0] => 4 [1] => 37 [2] => 2925 ) mystatusmessage2
What I would like to get it is in a single string value like this so that I can insert into database.
1,37,1035,1,mystatusmessage1
4,37,2925,2,mystatusmessage2
How can I achieve that. I'm trying to do with foreach but still I'm not able to do it.
Thanks,
Kimz
use implode function to make string from array for example
if you have array like Array('a','b','c');
implode(',',array('a','b','c') )
will return a,b,c as string
here first argument is your glue by which you want to join string
Here you go.
// Original array
$array = array(0 => 1, 1 => 37, 2 => 1035);
// $_POST array
$_POST = array(1,'mystatusmessage1');
// Jump to the end of array
end($array);
// Merge the post with original array
$newArr = array_merge($array,$_POST);
// Impode
echo implode(",",$newArr);
Repeat with other array.
Hello i have an array in php
Array
(
[0] => red
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => white
)
and i want to sort it using that array
Array
(
[0] =>
[1] => 0
[2] => -1
[3] => -5
[4] => -5
[5] => 9
)
so i want the element with the greatest value on the second array to come first on the first array not its value from the second array but the element it self from the first array to move in the first position on the first array! The second bigger to the second place etc.. elements with the same value don't care me how they will be arranged!
the output i want to get is
Array
(
[0] => white
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => red
)
You can use array_multisort() :
$ar1 = array(/* your SO links */);
$ar2 = array(/* your numbers */);
array_multisort($ar2, SORT_DESC, $ar1);
Documentation here
Use array_multisort.
see http://www.php.net/manual/fr/function.array-multisort.php, follow the "Exemple #1 Trier plusieurs tableaux"
Cordially
Lets call your arrays are $dataArray, and $sortArray respectively
asort($sortArray);
foreach ( $sortArray as $key=>$val ) {
$newArray[] = $dataArray[$key];
}
If you need it reversed, just add in a call to array_reverse() at the end.
I think what the OP wants is to sort the first array by the second array's values.
IE- the second array is the vote count for the first array's web pages.
This could be solved more simply if in the first array you have each element as an array with two elements in it, webpage & vote count
$pages = Array
(
array(
'page' => 'http://stackoverflow.com/questions/640805/open-source-ios-components-reusable-views-controllers-buttons-table-cells-e',
'count' => 0
)
array(
'page' => 'http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-for-iphone-ipad-ios-tips-and-hints',
'count' => -1
)
// etc for the rest...
)
But since the question asked how to do it in the current data structure:
$sorted_values = asort($pages);
$new_pages = array();
$sorted_keys = array_keys($sorted_values);
foreach($sorted_keys as $page_key)
array_push($new_pages, $pages[$page_key]);
I have three arrays each having SimpleXML Objects in them. They are structured like so:
Array
(
[0] => SimpleXMLElement Object
(
[post_id] => 1476
[name] => Johnson Fisheries Ltd.
[owner] => Mr. John Johnson
)
)
I want to be able to compare all 3 arrays and filter out the differences so that the results have only the elements that are the same in all 3 arrays.
For example:
Array1
(
[0] => 1476
[1] => 1560
[2] => 1342
)
Array2
(
[0] => 2454
[1] => 1476
)
Array3
(
[0] => 3412
[1] => 7512
[2] => 2454
[4] => 1476
)
The resulting array would only contain [0] => 1476
What's the best way to do this? I've looked for a function that will compare arrays in this way but I have had no luck. Any ideas?
Any help is very appreciated!
Best option will be using php's built-in array-intersect function.
$answer = array_intersect($Array1,$Array2,$Array3);
Loop over your first array, checking it against the 2nd and 3rd using in_array, like this:
foreach($array1 as $post_id) {
if(in_array($post_id, $array2) && in_array($post_id, $array3)) {
// We have a winner!
}
}
create a new array.
add all elements of the first array into the new array.
iterate through all the other elements, find out which elements in your new array are in the array to be compared. remove all the elements that don't appear.
at the end of your iterations, you have your desired array.