Need the array at index zero in some different manner - php

$i=0;
foreach($tagss as $tagdetails)
{
if($i==0)
$tags_array[]["00"]=array("id"=>$i,"name"=>"all","type"=>"gift_finder","parent_id"=>null,"url"=>"all","readonly"=>"0","deleted"=>"0");
$tags_array[][$tagdetails->id]=array("id"=>$tagdetails->id,"name"=>$tagdetails->title,"type"=>"gift_finder","parent_id"=>null,"url"=>$tagdetails->title,"readonly"=>"0","deleted"=>"0");
$i++;
}
echo json_encode($tags_array);
my out put of above code is :-
[{"00":{"id":0,"name":"all","type":"gift_finder","parent_id":null,"url":"all","readonly":"0","deleted":"0"}},{"1":{"id":"1","name":"Adventure","type":"gift_finder","parent_id":null,"url":"Adventure","readonly":"0","deleted":"0"}},{"2":{"id":"2","name":"cool","type":"gift_finder","parent_id":null,"url":"cool","readonly":"0","deleted":"0"}}]
that is the right but i want the out put like (just need 0 instead of 00)
so i tried :-
$i=0;
foreach($tagss as $tagdetails)
{
if($i==0)
$tags_array[]["0"]=array("id"=>$i,"name"=>"all","type"=>"gift_finder","parent_id"=>null,"url"=>"all","readonly"=>"0","deleted"=>"0");
$tags_array[][$tagdetails->id]=array("id"=>$tagdetails->id,"name"=>$tagdetails->title,"type"=>"gift_finder","parent_id"=>null,"url"=>$tagdetails->title,"readonly"=>"0","deleted"=>"0");
$i++;
}
echo json_encode($tags_array);
ouput of the above code :-
[[{"id":0,"name":"all","type":"gift_finder","parent_id":null,"url":"all","readonly":"0","deleted":"0"}],{"1":{"id":"1","name":"Adventure","type":"gift_finder","parent_id":null,"url":"Adventure","readonly":"0","deleted":"0"}},{"2":{"id":"2","name":"cool","type":"gift_finder","parent_id":null,"url":"cool","readonly":"0","deleted":"0"}}]
that is logically right that it is put the first element in the array and treat next element at first index but i need 0 index separately
Any suggestion please
thanks in advance .
in summary I need
[{"0":{"id":0,"name":"all","type":"gift_finder","parent_id":null,"url":"all","readonly":"0",
"deleted":"0"}},
{"1":"id":"1","name":"Adventure","type":"gift_finder","parent_id":null,"url":"Adventure","readonly":"0","deleted":"0"}},
{"2":"id":"2","name":"cool","type":"gift_finder","parent_id":null,"url":"cool","readonly":"0","deleted":"0"}
}]

You just need to pass option to your json_encode() call. It is JSON_FORCE_OBJECT:
echo json_encode($tags_array, JSON_FORCE_OBJECT);
then you'll able to result in an object for numeric keys (so you don't need to pass keys from PHP at all). Please, note, that is available since PHP 5.3

Related

hide anchor for array elements

I have two arrays like given below from these two arrays I am getting
missing element that is also given below:
$news=[1,2,3,4,5,6,7];
$new=[1,2,4,6,7];
$missing=[3,5];
I want when I have two values or multi-values in that case if the condition
should work and for another value else part to be executed
if(in_array($news,$missing))
{
echo "ok";
}
else
{
echo "no";
}
case would be :[3] ,[5] and [3,5];
I want if array value =3 output should be no if value=5 output should
be=no if both then also output should be no but when value is
different like [4] or [4,6] output would be ok;
can anyone please help me related this I am stuck here. Or is there any way to solve this kind of problems
You can use array_intersect()
count(array_intersect($news,$missing))
If count > 0 means there is atleast one element in both arrays, so answer will be no. It count = 0, means both arrays have different values, so answer will be ok.

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

Calling an array element inside another array

I think this is a naive question, but I can't find the proper syntax.
I have this code:
for ($i=1; $i<count($MyArray1); $i++){
$element=$MyArray1[$i];
$foo = $AnotherArray[$element];
echo $foo;
}
How can I skip the second line? I mean, the third line to be something like
$foo = $AnotherArray[$MyArray1[$i]];
for ($i=1; $i<count($MyArray1); $i++){
echo $AnotherArray[$MyArray1[$i]];
}
You can skip a fair amount of that code to make it a bit clearer. Firstly use foreach instead of for as it's a much more reliable way of iterating over arrays. Secondly I've broken down what you're trying to do, to simplify how you're getting it. Basically using the values of one array as the keys of another. So how to do it in three lines:
foreach(array_intersect_key($AnotherArray, array_flip($MyArray1)) as $value) {
echo $value;
}
This is using the excellent array_intersect_key method to grab all of the values from $AnotherArray with keys that match in the other array. As you want to use the values, we use array_flip to swap the keys and values, then just loop over the result and echo it.

count of array elements in php

I m trying to display no of elements in an array,but its showing 1 when i m trying to print the count
<?php
if(!$xml=simplexml_load_file('sunglasses.xml')){
trigger_error('Error reading XML file',E_USER_ERROR);
}
$array1=array();
foreach($xml as $syn)
{
for($i=0;$i<count($syn->productId);$i++)
{
$array1=$syn->productId;
}
}
echo count($array1, COUNT_RECURSIVE);
?>
There are 10 products in the xml file.So i want the count to be 10,but its printing only 1.
Please tell me whats wrong in the code.
Your formatting of code is horrible.
And yes, there might be 10 elements in the xml, but there is only one in $array1 because only the last assignment is present. You never told to $array1 to push values into the array, despite the fact you declared it as array. Assignation process = does not push values, if explicitly not called via array_push() or [] syntax.
$array1[] = $syn->productId;
Should work.
You should have tested the result at least. var_dump()'ing the $array1 will give you result of only one value - the last value, and you would be able to understand that something is wrong.
Following Change in code will resolved your issue.
<?php
if(!$xml=simplexml_load_file('sunglasses.xml')){
trigger_error('Error reading XML file',E_USER_ERROR);
}
$array1=array();
foreach($xml as $syn)
{
for($i=0;$i<count($syn->productId);$i++)
{
// here need to add the result in array. not to assign value directly to array object.
$array1[] = $syn->productId;
}
}
echo count($array1, COUNT_RECURSIVE);
?>

Returning string value in php array

Its a simple problem but i dont remember how to solve it
i have this array:
$this->name = array('Daniel','Leinad','Leonard');
So i make a foreach on it, to return an array
foreach ($this->name as $names){
echo $names[0];
}
It returns
DLL
It returns the first letter from my strings in array.I would like to return the first value that is 'Daniel'
try this one :
foreach ($this->name as $names){
echo $names; //Daniel in first iteration
// echo $names[0]; will print 'D' in first iteration which is first character of 'Daniel'
}
echo $this->name[0];// gives only 'Daniel' which is the first value of array
Inside your loop, each entry in $this->name is now $names. So if you use echo $names; inside the loop, you'll print each name in turn. To get the first item in the array, instead of the loop use $this->name[0].
Edit: Maybe it makes sense to use more descriptive names for your variables.
For example $this->names_array and foreach ( $this->names_array as $current_name ) makes it clearer what you are doing.
Additional answer concerning your results :
You're getting the first letters of all entries, actually, because using a string as an array, like you do, allows you to browse its characters. In your case, character 0.
Use your iterative element to get the complete string everytime, the alias you created after as.
If you only want the first element, do use a browsing loop, just do $this->name[0]. Some references :
http://php.net/manual/fr/control-structures.foreach.php
http://us1.php.net/manual/fr/language.types.array.php

Categories