This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 3 years ago.
I am getting data into two arrays and i want to push that data into single array one by one. Let say i want to get first element from first array and first element from second array and then push that data into single array.
first array
$url = $a->attr['href'];
$lnk[] = ['url'=>$url];
second array
$img = $img->attr['src'];
$img[] = ['img'=>$img];
I want this
$data[] = ['url'=>$url, 'img'=>$img];
I assume the size is same so you can iterate over the length on any of the arrays.
$finalArray=[];
for($i=0;$i<count($lnk);$i++){
$finalArray[] = ['url'=>$lnk[$i]['url'], 'img'=>$img[$i]['img']];
}
Related
This question already has answers here:
PHP add elements to multidimensional array with array_push
(4 answers)
Closed 9 months ago.
I am iterating through an array of arrays that contains sales data returning from a MySQL query:
$result = mysql_query("select salespersonId,grossProfit FROM sales");
foreach ($result as $line) {
$salespersonId = $line['salespersonId'];
$grossProfit = $line['grossProfit'];
}
I want to push the line into separate arrays per salespersonId (so that the 10 lines belonging to salespersonId 1 are in one array, and the 10 lines belonging to salespersonId 2 are in a different array) but I cannot seem to figure out how to do this. I've tried
things like:
array_push(${'data'.$salespersonId},$line); to no avail. I feel like I might need an associative array of arrays, but you can't array_push into associative arrays. What am I missing?
You can do something like this
$result = mysql_query("select salespersonId,grossProfit FROM sales");
$arr = [];
foreach ($result as $line) {
if (empty($line['salespersonId'])) {
continue;
}
$arr[$line['salespersonId']][] = $line;
}
print_r($arr);
This question already has answers here:
How to parse a CSV file using PHP [duplicate]
(6 answers)
Closed 2 years ago.
I am currently working on php page creating, and there is one requirement that I need to work on but I am stuck.
There is accounts.txt which has n rows of : date,time,id,status and 4 columns (date,time,id,status).
And basically, I need to split them by "\n" and then "," in order to make each a value for table with row and column equal to the # of adminlog.txt file.
'''
$data = file_get_contents($file_path.'/adminlog.txt');
$data = explode(",",$data);
$adminlog = array();
for ($i = 0; $i < sizeof($data); $i ++) {
array_push($adminlog,$data[$i]);
}
print($adminlog);
'''
Above was the code that I was trying to put in php in order to separate them by comma, but when I tried printing out $adminlog on my page, it would just show "Array". Is there actually way to separate the dataset neatly by "comma" and actually use the separated data and use it to create a table containing all information via loop?
Thanks so much in advance for y'all's enthusiasm and passion!
You can use print_r or var_dump or loop throught the array and print the values
$arr = array('Foo', 'Bar');
foreach($arr as $value) {
echo "$value\n";
}
This question already has answers here:
PHP - Grab the first element using a foreach
(10 answers)
Closed 5 years ago.
how can I get first item from db by foreach
$posts = new Posts();
$post = $posts->feature_post($conn);
foreach($post as $feature) { ?>
my html code is different for 1st item. so I need to get 1st item then other item,
how I can do it?
thanks in advance.
To get the first item of an array, you can use the reset function
http://php.net/manual/fr/function.reset.php
<?php
$posts = new Posts();
$listPost = $posts->feature_post($conn);
$firstPost = reset($listPost);
...
Also if you want to know if you loop through the first element and if the keys of your arrays are 0,1,2,3 etc..
<?php
foreach($array as $key => $cell) {
if ($key === 0) {
// this is your first element
....
}
}
If the keys of your array are not numeric indexes but you don't intend to use them, you can obtain such array by using the array_values function
This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
Closed 6 years ago.
I have the following code that loops trough an array called projects and each project is an associative array. Then I get the image properties and then I want to add a new element to this associative array with the image properties. But it doesn't get added.
foreach ($projects as $project) {
$image_dimensions = array(getimagesize('data/'.$project['base_image']));
$project['image_dimensions'] = $image_dimensions;
}
Why isn't $project['image_dimensions'] getting added to $project?
Please try with this one.
You need to add with all projects all key.
foreach ($projects as $key => $project) {
$image_dimensions = array(getimagesize('data/'.$project['base_image']));
$projects[$key]['image_dimensions'] = $image_dimensions;
}
This question already has answers here:
get a single value of array items in php
(3 answers)
Closed 7 years ago.
I am having a bit of trouble getting a single item shown/echoed in a foreach loop. What I mean I have an array (filled from database).
return array(
'FriendName' => $FriendName,
'FriendImage' => $FriendImage
);
What I want is to show only FriendName while in a loop.
I have tried and this is an example:
foreach($UserFriends as $item) {
echo $item['FriendName'];
}
But it shows NULL and not the data I expect.
No need for a loop if you just need one value, you can use this instead:
echo $UserFriends['FriendName']
Learn more about php Arrays
Its been better to use it directly instead of looping an array if its not multidimensional array you can simply get it using
$UserFriends['FriendName']
you can checked using if condition like
foreach($UserFriends as $key => $item) {
if($key == 'FriendName'){
echo $item;
}
}
your array is not multidimensional so you can access vlaues like that.
or else you can get a value directly like
echo $userFriends['FriendName'];
I hope this is working for you