Array Echo Together Each Value Together [duplicate] - php

This question already has answers here:
How can I loop through two arrays at once? [duplicate]
(2 answers)
Closed 6 years ago.
I have 2 Arrays
$name = Array ( [1] => Potrait Color Correction [2] => Extraction )
$number = Array ( [1] => 060716113223-13555 [2] => 49101220160607-25222 )
I'm trying to print the index 1 of first array with index 1 of 2nd array and similary for index 2
This Is My code For Printing (think it's wrong)
foreach ($name as $abc => $val) {
foreach ($number as $xyz => $valu) {
if(!in_array($val, $arr)){
//echo $val." ";echo $valu;
$arr[]=$val;
}
}
}
Problem is my array number is printing only the first value Is Getting Repeated
for both
Potrait Color Correction 060716113223-13555
Extraction 060716113223-13555
im looiking for something like this TO Echo
Potrait Color Correction 060716113223-13555
Extraction 49101220160607-25222

Use for loop to access multiple array:
for($i=0;$i<count($name);$i++) {
echo $name[$i]." ".$number[$i]."<br />";
}
Output:
Potrait Color Correction 060716113223-13555
Extraction 49101220160607-25222

Simply use the index from the first and only foreach to reference the second array like this
$name = Array ( [1] => Potrait Color Correction [2] => Extraction )
$number = Array ( [1] => 060716113223-13555 [2] => 49101220160607-25222 )
The code
$arr = [];
foreach ($name as $idx => $val) {
if(!in_array($val, $arr)){
echo $val . ' ' . $number[$idx] . '<br>';
$arr[]=$val;
}
}
Or if this is a CLI script use PHP_EOF instead of <br>

Related

How to generate combinations two element of array in PHP [duplicate]

This question already has answers here:
PHP - Generate all combinations of items in array
(2 answers)
Closed 3 years ago.
I have an array() contains string F1, F2, F3, F4.........
<?php
$facts= array("F1", "F2", "F3", "F4);
?>
How can I generate combinations two elements of that array.
The output can be like that:
F1.F2
F1.F3
F1.F4
F2.F3
F2.F4
F3.F4
Please help me
Try this:
Solution
$facts= array("F1", "F2", "F3", "F4");
$new_array = array();
foreach($facts as $key => $val){
foreach($facts as $key2 => $val2){
if($key2 <= $key) continue;
$new_array[] = $val . '.' . $val2;
}
}
print_r($new_array);
Output
Array
(
[0] => F1.F2
[1] => F1.F3
[2] => F1.F4
[3] => F2.F3
[4] => F2.F4
[5] => F3.F4
)

PHP Array - obtaining vlaues

I have an array, the output of print_r is:
Array ( [0] => Array ( [ID] => 1 [col1] => 1 ) [1] => Array ( [ID] => 2 [col1] => 2 ) )
Could you help of kind of array it is? So that I could research more about it? What I want is to get ID and col1 values
I've tried to use foreach:
foreach($array_name as $key=>$value){
print "$key holds $value\n";
}
The output I get is 0 holds Array 1 holds Array
And I would simply like to get:
1 1
2 2
It's a multi dimensional array, or an array where each element is another array. So you'll need to loop twice. Try this to look at it:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$key = $val\n";
}
}
Or, to get your just added desired output, do this:
foreach($array_names as $arr)
{
foreach($arr as $key => $val)
{
print "$val ";
}
print "\n";
}
Or this:
foreach($array_names as $arr)
{
print $arr['ID'] . " " . $arr['col1'] . "\n";
}
or a few other ways but you should be getting the picture.

Get values from custom array using php

I have got the below array
Array
(
[0] => Array
(
[0] => Contact Number
[1] => 35443545
)
[1] => Array
(
[0] => Address
[1] => vvvv
)
)
I would like to display as
Contact Number
35443545
<hr>
Address
vvvv
My code
foreach($address_box_content as $k=>$address)
{
echo '<h2>'.$address[$k].'</h2><p>'.$address[$k+1].'</p>';
}
But for some reason it is printing the first 2 lines and then displaying a notice 'Undefined offset:2'
What you have is an array. One whose elements are (also) arrays. Each array that you have stored has 2 elements, 0 and 1.
When you loop, $k is the index of the main (outer) array. Its value doesn't make any sense in the inner array. You just need to loop over the outer array, and print the 0 and 1 elements from the inner one.
foreach($address_box_content as $address)
{
echo '<h2>'.$address[0].'</h2><p>'.$address[1].'</p>';
}
Well you're getting undefined offset error because you're using $k which is the index of the outer array
You can do something like:
foreach($address_box_content as $addresses){
foreach($addresses as $address){
echo '<h2>', $address, '</h2><p>', $address, '</p>';
}
}
And if you want to get the index of inner array:
foreach($address_box_content as $addresses){
foreach($addresses as $key => $address){
echo '<h2>', $address[$key], '</h2><p>', $address[$key], '</p>';
}
}
This is your array
$data = Array
(
[0] => Array
(
[0] => Contact Number
[1] => 35443545
)
[1] => Array
(
[0] => Address
[1] => vvvv
)
)
simple way to print array
for($i=0;$<count($data);$i++)
{
echo "<h2>".$data[$i][$0]."</h2>"."<p>".$value[$i][1]."</p>"."<hr />";
}
well, for each element you define a key and a value which are $k and $address.
$k will be 0 and 1, $address will be 0 and 1, and then 0 and 1.
the undefined offset error is because you call $k plus 1 that on the second foeach iteration try to access to position 1+1 (2) doesn't find anything.
you could use something like:
foreach($address_box_content as $data => $value) {
echo "<h2>" . $value[0] . "</h2>"
. "<p>" . $value[1] . "</p>"
. "<hr />";
}

PHP Get the first value of all arrays in a multidimensional array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 1 year ago.
Here's a section my multidimensional array:
Array (
[0] => Array ( [0] => Height [1] => 40 )
[1] => Array ( [0] => Weight [1] => 15 )
[2] => Array ( [0] => Ctr_Percent [1] => 15 )
)
What would the syntax be for just printing height, weight, and ctr_percent? I don't mean echoing it like:
echo $array[0][0];
echo $array[1][0];
Is there a way to iterate through the entire multidimensional array and echo out the first value of each child array?
Supposing you use php 5.3:
$first_elements = array_map(function($i) {
return $i[0];
}, $data);
Otherwise you need to implement a callback function or just use plain old foreach
Here is a one-liner:
array_map('array_shift', $array);
Will return:
Array
(
[0] => Height
[1] => Weight
[2] => Ctr_Percent
)
And here is another one:
array_combine(array_map('array_shift', $temp), array_map('array_pop', $temp))
Will return:
Array
(
[Height] => 40
[Weight] => 15
[Ctr_Percent] => 15
)
Use array_column:
$result = array_column($array, 0);
foreach ($main_array as $inner_array){
echo $inner_array[0] . "\n";
}
foreach($array as $x) {
echo $x[0]."\n";
}
I think the function your looking for is reset() e.g.
array_map('reset', $array);
or
foreach ($array as $subarray)
echo reset($subarray)."\n";
Note that this works even if 0 is not the first index of the array. E.g. $a = [1=>5,0=>3]; echo reset($a); would still echo 5;.
The easiest way to do it using array_walk
function getFirstElement(&$val){
$val = $val[0];
}
array_walk($data,'getFirstElement');
Now if you print $data like print_r($data); you will get result as below
Array
(
[0] => Height
[1] => Weight
[2] => Ctr_Percent
)

Proper Syntax for PHP foreach function with associative arrays

I have the following array named $ingredient_difference in PHP (example output below):
Array (
[total_remaining_ingredients] => Array (
[0] => 2 [1] => 3 [2] => 10
)
[idrecipe] => Array (
[0] => 8 [1] => 10 [2] => 9
)
[value] => Array ( [0] => 1 [1] => 1 [2] => 1 )
)
I'm trying to extract at least the values of idrecipe using 'foreach', but I'm getting undefined index with the following code:
foreach($ingredient_difference as $recipe_output)
{
echo $recipe_output['idrecipe']."<br />";
}
I know the above isn't exactly the way to do it, but this also was not working (undefined index error for 'idrecipe', 'value' and 'total_remaining_ingredients'):
foreach($ingredient_difference as $c => $rowkey)
{
$sorted_idrecipe[] = $rowkey['idrecipe'];
$sorted_value[] = $rowkey['value'];
$sorted_remaining_ingredients[] = $rowkey['total_remaining_ingredients'];
}
What am I missing in my foreach syntax? Or is there a better way?
This foreach construct is also giving undefined index errors:
foreach($ingredient_difference as $rowkey => $index_value)
{
$id_value[$key] = $index_value['idrecipe'];
$value_value[$key] = $index_value['value'];
$tri_value[$key] = $index_value['total_remaining_ingredients'];
}
Answer thanks to ComFreek:
$result_ingredient_difference = array();
$count_id = count($ingredient_difference['idrecipe']);
for ($i=0; $i<$count_id; $i++)
{
$result_ingredient_difference[] = array(
'tri' => $ingredient_difference['total_remaining_ingredients'][$i],
'idrecipe' => $ingredient_difference['idrecipe'][$i],
'value' => $ingredient_difference['value'][$i]
);
}
//rearranged array of $result_ingredient_difference able to call proper indexing with the below
foreach($result_ingredient_difference as $rowkey => $index_value)
{
$id_value[$key] = $index_value['idrecipe'];
$value_value[$key] = $index_value['value'];
$tri_value[$key] = $index_value['tri'];
}
With your first foreach() loop, you iterate over the main array and not over the values of the subarray idrecipe!
foreach($ingredient_difference['idrecipe'] as $value)
{
echo $value;
}
foreach constructs a loop. In your code
foreach($ingredient_difference as $recipe_output) {
echo $recipe_output['idrecipe']."<br />"; }
in the first loop run: $recipe_output is $ingredient_difference[total_remaining_ingredients]
in the second loop run: $recipe_output is $ingredient_difference[idrecipe]
in the third loop run: $recipe_output is $ingredient_difference[value]
because there is no
$ingredient_difference['total_remaining_ingredients']['idrecipe']
$ingredient_difference['idrecipe']['idrecipe']
$ingredient_difference['value']['idrecipe']
you get the error.
to see how the foreach loop works use the examples on http://php.net/manual/de/control-structures.foreach.php
i Expect what you want to do is:
foreach($ingredient_difference['idrecipe'] as $value_of_recipe)
{
echo $value_of_recipe."<br />";
}

Categories