Handling PHP Array post values and merging - PHP Arrays - php

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.

Related

i would like to merge this array such that its one array

<?php
i have decalered the array before the for loop
$sample=array();
I am Fetching database data which is in form of eg 5,6,2 i had imploded the before and stored them
$myseats= $db->displaySeats($showtime_id);
foreach ($myseats as $key) {
$bookedseats= $key['seats_booked'];
Exploding the values to $test variable
$test=explode(",", $bookedseats);
array_push($sample,$test);
}
print_r($sample) ;
?>
the problem is I am getting this output
Array ( [0] => Array ( [0] => 22
[1] => 26
[2] => 27
[3] => 37
[4] => 38
[5] => 41
)
[1] => Array ( [0] => 30
[1] => 31
[2] => 32
)
)
that is two arrays inside an array..the database has this two entries which am using a test
You can use array_merge
PHP docs
Instead of pushing arrays into an array, use array_merge() to place all these items into a single array as part of your loop
$sample = [];
foreach ( $rows as $bookedseats ){
$test=explode(",", $bookedseats);
$sample = array_merge($sample, $test);
}

PHP removing an array element and inserting it into another array

I have 2 arrays which are in the same form as below; for examples sake, lets call them $array1 and $array2
Array (
[Element1] => Array
(
[id] => 11
[morethings] => 145
[somemore] => namehere
)
[Element2] => Array
(
[id] => 11
[morethings] => 145
[somemore] => namehere
)
[Element3] => Array
(
[id] => 11
[morethings] => 145
[somemore] => namehere
)
)
What I need to do is take Element2 from the first array and then insert it into array2 as NewElement2
I have the following below but it keeps returning nothing at all in array2
$searchArray = array_search('Element2', $array1);
array_splice($array2, $searchArray, 1, array('NewElement2'));
Any help would be greatly appreciated.
for assign the value you can simply
$array2['Element2'] = $array1['Element2'];
(this append or repalce the entry for Elemet2 in $array2)
and for remove the value
unset($array1['Element2']);

Why I don't need to use array_push in associative array?

I was using this code, but If I used array_push() it was inserting values with null, I was using array_push to enter values in the array
foreach ($_POST['record_num'] as $check_rec_num) {
if(!in_array($check_rec_num, $_SESSION['selected_record'][$pageno])) {
array_push($_SESSION['selected_record'][$pageno][], $check_rec_num);
}
}
but when I used this it was automatically adding values in the array, without using array_push why is that so?
foreach ($_POST['rec_num'] as $check_rec_num) {
if(!in_array($check_rec_num, $_SESSION['selected_record'][$pageno])) {
$_SESSION['selected_record'][$pageno][] = $check_rec_num;
}
}
1st example
Array ( [1] => Array ( [0] => 36 [1] => 35 ) [2] => )
2nd example (without bar brackets)
Array ( [1] => Array ( [0] => 36 [1] => 35 [2] => 34 ) [2] => Array ( [0] => ) )
Array Design 3rd example without using array_push how the hell it is adding the values automatically at the end of the array without array_push?
Array (
[1] => Array (
[0] => 36
[1] => 35
)
[2] => Array (
[0] => 33
[1] => 32
)
)
You have an extra [] in
array_push($_SESSION['selected_record'][$pageno][], $check_rec_num);
This will do it:
$_SESSION['selected_record'][$pageno] = array();
array_push($_SESSION['selected_record'][$pageno], $check_rec_num);
See the manual on array_push.
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
Yet, you should better use $_SESSION['selected_record'][$pageno][] since
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.
That's because of the extra []
Remove it and it'll work:
array_push($_SESSION['selected_record'][$pageno], $check_rec_num);

PHP sort array using another array no keys

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]);

Change value in first of two arrays (multidimensional) PHP

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);

Categories