Suppose I have an array that looks like this
array ([apple] => 1, [dog]=>2, [cat]=>5, [bread]=>9, [shoes]=> 4)
Is it possible for me to print the first 3 values of the array? If so, how? Any ideas?
$firstThreeElements = array_slice($array, 0, 3);
Where 0 is your offset and 3 is the number of elements you want.
There are many ways.
list( $first, $second, $third) = $array;
echo $first . ' ' . $second . ' ' . $third;
Or:
echo array_shift( $array);
echo array_shift( $array);
echo array_shift( $array);
Or:
$i = 0;
foreach( $array as $el) {
if( $i >= 3) break;
echo $el;
$i++;
}
Or:
foreach( array_slice( $array, 0, 3) as $el) {
echo $el;
}
Or:
echo implode( ' ', array_slice( $array, 0, 3));
Using iterators:
$array = ['apple' => 1, 'dog' => 2, 'cat' => 5, 'bread' => 9, 'shoes' => 4];
foreach (new LimitIterator(new ArrayIterator($array), 0, 3) as $key => $val)
{
echo "$key => $val\n";
}
Related
If I have the following:
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $v) {
echo $v;
}
How can I make it output:
2, 1, 3, 17
Quoting the PHP Manual on Language Operators:
The + operator returns the right-hand array appended to the left-hand
array; for keys that exist in both arrays, the elements from the
left-hand array will be used, and the matching elements from the
right-hand array will be ignored.
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
$b = array_values($a);
echo implode(', ', array($b[1], $b[0]) + $b), PHP_EOL;
Output:
2, 1, 3, 17
$values = array_values($a);
echo "{$values[1]}, {$values[0]}, "
foreach (array_slice($values, 2) as $v){
echo "$v, "
}
If you care about last comma...
$values = array_values($a);
echo "{$values[1]}, {$values[0]}, "
$lastIndex = count($values) - 1;
foreach (array_slice($values, 2) as $k => $v){
echo $v;
if ($k != $lastIndex){
echo ", ";
}
}
You could probably do something like:
<?php
$my_array = array(...);
$keys = array_keys($my_array);
$second_key = $keys[1]; // if your array can be whatever size, probably want to check that first
echo $my_array[$second_key];
foreach ($my_array as $key => $value) {
if ($key == $second_key) {
continue;
}
echo $value;
}
?>
I have two array
$array1 = array
(
array('A',0),
array('B',0),
array('C',0),
array('D',0),
array('E',0),
array('F',0),
)
$array2 = array
(
array('A',5),
array('B',6),
array('C',10),
array('F',23),
)
$array2 will be changing sometimes A keys is there or its not there. It is applied for all keys.
I want to create a new array or replace the array values in $array1 to
$array1 = array
(
array('A',5),
array('B',6),
array('C',10),
array('D',0),
array('E',0),
array('F',23),
)
Try something like below
if(count($array1) > count($array2)){
$tempArr1 = $array1;
$tempArr2 = $array2;
}else{
$tempArr1 = $array2;
$tempArr2 = $array1;
}
$newArr = array();
foreach($tempArr1 as $values){
$a = $values[0]; $n = $values[1];
foreach($tempArr2 as $key=>$val){
if($val[0] == $a){
$n = ($val[1] > $n) ? $val[1] : $n;
unset($tempArr2[$key]);
}
}
$newArr[] = array($a, $n);
}
print_r($newArr);
$array1 = array (
array('A',0),
array('B',0),
array('C',0),
array('D',0),
array('E',0),
array('F',0),
);
$array2 = array (
array('A',5),
array('B',6),
array('C',10),
array('F',23),
);
foreach( $array2 as $itemKey2 => $itemVal2 ) {
$found = false;
foreach( $array1 as $itemKey1 => $itemVal1 ) {
if( $itemVal1[0] == $itemVal2[0] ) {
$found = true;
$array1[$itemKey1][1] = $itemVal2[1];
break;
}
}
if( !$found )
$array1[] = $item2;
}
echo var_export( $array1, true );
In retrospect, this scenario seems needlessly complicated. Unless something else truly requires this structure, if possible use something like:
$array1 = array (
'A' => 0,
'B' => 0,
'C' => 0,
'D' => 0,
'E' => 0,
'F' => 0
);
$array2 = array (
'A' => 5,
'B' => 6,
'C' => 10,
'F' => 23
);
foreach( $array2 as $key => $val ) {
$array1[$key] = $val;
}
I have a simple associative array.
<?php
$assocArray = array('a' => 1, 'b' => 2, 'c' => 3);
?>
Using only while loop, how can I print it in this result?
$a = 1
$b = 2
$c = 3
This is my current solution but I think that this is not the efficient/best way to do it?
<?php
$assocArray = array('a' => 1, 'b' => 2, 'c' => 3);
$keys = array_keys($assocArray);
rsort($keys);
while (!empty($keys)) {
$key = array_pop($keys);
echo $key . ' = ' . $assocArray[$key] . '<br />';
};
?>
Thanks.
try this syntax and this is best efficient way to do your job...........
while (list($key, $value) = each($array_expression)) {
statement
}
<?php
$data = array('a' => 1, 'b' => 2, 'c' => 3);
print_r($data);
while (list($key, $value) = each($data)) {
echo '$'.$key .'='.$value;
}
?>
For reference please check this link.........
Small Example link here...
The best and easiest way to loop through an array is using foreach
foreach ($assocArray as $key => $value)
echo $key . ' = ' . $value . '<br />';
Try this;
$assocarray = array('a' => 1, 'b' => 2, 'c' => 3);
$keys = array_keys($assocarray);
rsort($keys);
while (!empty($keys)) {
$key = array_pop($keys);
echo $key . ' = ' . $assocarray[$key] . '<br />';
};
I have a simple solution for this, it will get the job done..
$x = array(0=>10,1=>11,2=>"sadsd");
end($x);
$ekey = key($x);
reset($x );
while(true){
echo "<br/>".key($x)." = ".$x[key($x)];
if($ekey == key($x) )break;
next($x);
}
Try code below:
$assocArray = array('a' => 1, 'b' => 2, 'c' => 3);
$obj = new ArrayObject($assocArray);
foreach ( $obj as $key => $value ) {
echo '$' . $key .'='. $value . "<br/>";
}
I have a array:-
Array ( [6] => 1 [6(HL)] => 3 [5] => 1 [7(HL)] => 2 )
How to break it and echo as like this:-
2(6), 3(6(HL)), 1(5), 2(7(HL))
I have try to use implode to break it as a string, but this is what result I get:-
2, 3, 1, 2
any idea on this?
Thanks for advance.
assume your array is $arr :
$output = '';
foreach($arr as $k => $v) {
$output .= $v . '(' . $k . ')' . ', ';
}
$output = substr($output, 0, strlen($output)-2);
echo $output;
$s = implode(', ', array_map(function($a, $b) {
return "$b($a)";
}, array_keys($a), array_values($a)));
Or
$s = '';
foreach ($a as $key => $val)
{
if ($s) $s .= ', ';
$s .= "$val($key)";
}
Out of this:
$arr = array(
array('boo', 4),
array('boo', 1),
array('foo', 2),
array('foo', 6)
);
how best calculate into this?:
$arr = array(
'boo' => 5,
'foo' => 8
);
$sum = array();
for ( $i = 0; $i < count( $arr ); $i++ )
{
if ( !isset( $sum[ $arr[$i][0] ] )
$sum[ $arr[$i][0] ] = 0;
$sum[ $arr[$i][0] ] += $arr[$i][1];
}
print_r( $sum );
$arr = array(
array('boo', 4),
array('boo', 1),
array('foo', 2),
array('foo', 6)
);
Then:
$arr2 = array();
foreach($arr as $value) {
if(isSet($arr2[$value[0]])) $arr2[$value[0]] += $value[1];
else $arr2[$value[0]] = $value[1];
}