How to use echo inside array subscript - php

<?php
$i=0;
if( $free_form_fields != '' ) :
foreach ($free_form_fields as $fields)
{
$parameters = json_decode($fields->parameters);
// echo $i;
echo "{$form_field_order[$i]}";
// if($form_field_order[$i] == $fields->id)
// {
echo "<li class='ui-state-default' id={$fields->id}>{$parameters->label}</li>";
// }
$i = $i+1;
}
endif;
?>
I want to check something like this if($form_field_order[$i] == $fields->id)
but it is giving me an error
> Undefined offset: 2
so basically how can i access the $i th value (0,1...) of array $from_field_order?
Edit
print_r($form_field_order);die(); gives an array
Array ( [0] => 2 [1] => 1 )

The array contains only two elements (0,1 indexed). It tries to access index 2 and throws this error as it is not existing. So Put a condition to check if the element exists using isset($form_field_order[$i]) before accessing that

Try this,
<?php
$i=0;
if( count($free_form_fields) > 0 ) :
foreach ($free_form_fields as $fields)
{
$parameters = json_decode($fields->parameters);
// echo $i;
echo $form_field_order[$i];
// if($form_field_order[$i] == $fields->id)
// {
echo "<li class='ui-state-default' id={$fields->id}>{$parameters->label}</li>";
// }
$i = $i+1;
}
endif;
?>

Related

How can I delete data from my array with the key value?

I am trying to delete all the data from within a specific key in my array, but cant work out why my code isn't working. I have used print_r to check the code works and I can see that the correct value is being printed for each array when I click the 'remove' button, but it isn't removing the data from the key.
My current array looks like this:
Array
(
[0] => Array
(
[0] => S30813-Q100-X303
[1] => 5
[2] => Refurbished
)
[1] => Array
(
[0] => JX-1T1-LTU
[1] => 8
[2] => New
)
)
I am outputting the data to a table with:
for ($row = 0; $row < $totalcount; $row++) {
echo "<tr>";
for ($col = 0; $col < 3; $col++) {
echo "<td>".$contents[$row][$col]."</td>";
}
for ($col = 0; $col < 1; $col++) {
$del = $row;
echo "<td><form action='' method='post'><input type='text' name='del' value=".$row."></input><input type='submit' name='deletepart' value='Remove'></input></form></td>";
}
echo "</tr>";
}
Front-end:
My php to unset the array key (and where I am guessing the problem lies) is:
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
$value_to_delete = $key;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>
Any help on where I am going wrong here would be very much appreciated!
try this
write $key = $value_to_delete; instead of $value_to_delete = $key
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
//$value_to_delete = $key; //instead of this use
$key = $value_to_delete;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>

match two arrays and return count of how much values matched php

I have one array with values: $array_metaValue
Array ( [0] => php [1] => ajax [2] => my [3] => profile [4] => java )
and second array contains: $search_res[$e]
php
ajax
But the problem is that the count value is always one which is wrong. It should be 2.
print_r( $array_metaValue);
for($e=0;$e<=count($search_res);$e++){
echo '<br>'.$search_res[$e].'<br>';
echo '<pre>';
$key = array_search($search_res[$e],$array_metaValue);
if(!$key==0)
{
$count=$count+1;
}
$count right now is saving 1.
Use
$count = count(array_intersect($array_metaValue, $search_res));
array_intersect returns an array containing the elements that are in both of the input arrays.
The problem with your code is that you need to test
if ($key !== false)
Try this
$arrInp = array('php','ajax','my','profile','java');
$arrSearch = array('php','ajax');
$count = 0;
foreach ($arrSearch as $key => $value) {
if(in_array(trim($value), $arrInp))
$count++;
}
echo $count;
<?php
$array_metaValue=array('php','ajax','profile','java');
$search_res=array('php','ajax');
print_r($array_metaValue);
$count=0;
for($e=0;$e<count($search_res);$e++){
echo '<br>'.$search_res[$e].'<br>';
$key = array_search($search_res[$e],$array_metaValue);
echo 'Key value =>'.$key. " ";
if($key>=0)
{
$count=$count+1;
echo 'Count value =>'.$count;
}
}
?>

Array get 2nd item

foreach($xml->xpath( 'programme[#channel="1"]' ) as $item) {
if (something) {
echo "cat.: ".$item->{'category'}. "<br>";}
If i have 2x "category" to choose, how to set it to get the second one, not the 1st in the row?
The simplest way would be to use a counter variable:
$i = 0;
foreach($xml->xpath( 'programme[#channel="1"]' ) as $item) {
if ( $i++ != 1)
echo "cat.: ".$item->{'category'}. "<br>";
However, this will include every category after the first. If you only want the second category, you could use the same approach:
$i = 0;
foreach($xml->xpath( 'programme[#channel="1"]' ) as $item) {
if ( $i++ == 2)
echo "cat.: ".$item->{'category'}. "<br>";
This gives you the second category and also ensures that you'll get the first category when there's no second one:
foreach ($xml->xpath('programme[#channel="1"]') as $item) {
if (something) {
if (isset($item->category[1]) && !empty($item->category[1])) {
echo 'cat.: '.$item->category[1].'<br />';
} else {
echo 'cat.: '.$item->category[0].'<br />';
}
}
}

Echo only info - Session PHP

How can I only echo the information in a session that people added by them self? My script:
<?php
session_start();
$array = $_SESSION["wenslijst"];
if (isset($_POST["item"]) && (int)$_POST["item"] > 0)
{
if (!isset($_SESSION["wenslijst"]))
{
$_SESSION["wenslijst"] = array();
$_SESSION["aantal"] = array();
}
$i = 0 ;
while ($i < count($_SESSION["wenslijst"]) && $_SESSION["wenslijst"][$i] != $_POST["item"]) $i++;
if ($i < count($_SESSION["wenslijst"]))
{
$_SESSION["aantal"][$i]++;
}
else
{
$_SESSION["wenslijst"][] = $_POST["item"];
$_SESSION["aantal"][] = 1;
}
}
?>
When I add a product my array looks like this, I only echo the bold message: (see the long code as my spefic number for a product)
Array
(
[0] => Array
(
[f805862bbd430d0673f3f949249326f3] =>
)
)
Thanks all for your help.
Aantal means: count
Wenslijst means: wishlist
Do i understand you correctly, that your array looks like this?!
[0] => array(
"your_super_long_product_key" => array(),
"another_super_long_product_key" => array(),
"awesome_super_long_product_key" => array(),
)
So you could access the data like this:
foreach(array_keys($array[0]) as $key) {
echo $key . "\n";
}
This will give you:
your_super_long_product_key
another_super_long_product_key
awesome_super_long_product_key
To loop through your whole array like this use:
foreach($array as $dataset) {
foreach(array_keys($dataset) as $key) {
echo $key . "\n";
}
}

How to get a index of an array element

I need to get the index of the array element, in my case $ch is an array element, I need the index value (for e.g.:overview=array[0], $arval = 0), so i could print the $tabs[$arval+1].
<?php
$tab ='overview,gallery,video,songs$value1$value2$value3$value4';
$tabs = explode('$',$tab);
$tabname = explode(',',$tabs[0]);
echo '<div id="tab" style="float:left;width:100%;height:30px;background:#333">';
foreach($tabname as $i)
{
echo '<a id="'.$i.'" style="color:#fff;padding:2px 10px;" href="?tab='.$i.'" >'.$i.'</a>';
}
echo '</div>';
if(isset($_GET['tab']))
{
$ch=$_GET['tab'];
foreach($tabname as $i){
if ($ch == $i)
// get the array index of the current element $arval
// echo $tabs[$arval+1]
} } ?>
How can I accomplish it?
foreach($tabname as $index => $i){
^^^^^^^^^
Maybe this could work for you:
if(isset($_GET['tab']))
{
$ch=$_GET['tab'];
if($key = array_search($ch, $tabname, true))
// get the array index of the current element $arval
echo $tabs[$key];
}
}
In your foreach you need to do this:
foreach($tabname as $index => $value){
// $index is the index
// $value is the value
if ($ch == $i)
// get the array index of the current element $arval
// echo $tabs[$arval+1]
}

Categories