<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Array Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<atom:link href="https://atcodex.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>https://atcodex.com/tag/array/</link>
	<description>Achieve financial success without sacrificing well-being! AtCodex provides actionable strategies for managing money, time, and personal growth—all in one place.</description>
	<lastBuildDate>Sat, 28 Aug 2021 17:14:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://atcodex.com/wp-content/uploads/2020/05/cropped-New-Project-1-32x32.png</url>
	<title>Array Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<link>https://atcodex.com/tag/array/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Array to XML Conversion and XML to Array Convert in PHP</title>
		<link>https://atcodex.com/php/array-to-xml/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Sun, 11 Oct 2020 06:04:25 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1170</guid>

					<description><![CDATA[<p>Many times we need to store the data in XML format into the database or into the file. In this article, we are going to discuss how we can convert &#8230; </p>
<p>The post <a href="https://atcodex.com/php/array-to-xml/">Array to XML Conversion and XML to Array Convert in PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Many times we need to store the data in XML format into the database or into the file. In this article, we are going to discuss how we can convert array to XML and XML to array in PHP.</p>



<p>Nowadays we mainly use <strong>JSON </strong>format to store and send the data from one location to another. But what if we require to store or send the data in <a href="https://www.php.net/manual/en/simplexml.examples-basic.php" target="_blank" rel="noreferrer noopener">XML </a>format. </p>



<p>Let&#8217;s suppose we have to work with the third party database and to communicate to that database we have only XML API available there, then obviously we need to fetch those XML API and need to convert them in PHP array to work around. And the same thing applies when you need to provide some data using API to a third party when demands the API in XML format. In this article, we will learn how we can convert PHP simple array, associative array, or multidimensions array to XML format and vice versa.</p>



<p>If you are concerned about the size of the database, XML can help us to free some space from the database. You can use an XML file to save the data instead of saving data into the database you can save data in XML files and later when required you can get the data in an array using to XML to PHP conversion method that we have explained below.</p>



<h2 class="wp-block-heading">What is XML </h2>



<p><strong>XML stands</strong> for an extensible markup language. A markup language is a set of codes, or tags, that describes the text in a digital document. XML is a software- and hardware-independent tool for storing and transporting data.</p>



<ul class="wp-block-list"><li>XML stands for extensible Markup Language</li><li>XML is a markup language much like HTML</li><li>XML was designed to store and transport data</li><li>XML was designed to be self-descriptive</li><li>XML is a W3C Recommendation</li></ul>



<h2 class="wp-block-heading">The Difference Between XML and HTML</h2>



<p>XML and HTML were designed with different goals:</p>



<ul class="wp-block-list"><li>XML was designed to carry data &#8211; with a focus on what data is</li><li>HTML was designed to display data &#8211; with a focus on how data looks</li><li>XML tags are not predefined like HTML tags are</li></ul>



<p></p>



<h2 class="wp-block-heading">Convert PHP Multidimensional Array to the XML file format</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$array = array(
    'class'=> 'class 10th',
    'student'=> array(
      '0' => array(
        'name' 		=> 'David',
        'rollNo'		=> '34'
      ),
      '1' => array(
        'name' 		=> 'Sunny',
        'rollNo'		=> '30'
      ),
      '2' => array(
        'name' 		=> 'Illa',
        'rollNo'		=> '37'
      ),
      '3' => array(
        'name' 		=> 'Monna',
        'rollNo'		=> '35'
      )
    )
  );</pre>



<h3 class="wp-block-heading">Method for generating XML</h3>



<p>Now, you need to create a function generatXML()</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">
function generateXML($data) {
    $title = $data['class'];
    $rowCount = count($data['student']);
    
    //create the xml document
    $xmlDoc = new DOMDocument();
    
    $root = $xmlDoc->appendChild($xmlDoc->createElement("student_info"));
    $root->appendChild($xmlDoc->createElement("title",$title));
    $root->appendChild($xmlDoc->createElement("totalRows",$rowCount));
    $tabUsers = $root->appendChild($xmlDoc->createElement('rows'));
    
    foreach($data['student'] as $user){
        if(!empty($user)){
            $tabUser = $tabUsers->appendChild($xmlDoc->createElement('student'));
            foreach($user as $key=>$val){
                $tabUser->appendChild($xmlDoc->createElement($key, $val));
            }
        }
    }
    
    header("Content-Type: text/plain");
    
    //make the output pretty
    $xmlDoc->formatOutput = true;
    
    //save xml file
    $file_name = str_replace(' ', '_',$title).'.xml';
    $xmlDoc->save($file_name);
    
    //return xml file name
    return $file_name;
}
</pre>



<p>You only need to use <strong>generateXML</strong>() function and pass the data array in it to convert the array to XML in PHP. This function creates an XML document using DOMDocument class and inserts the PHP array content in this XML document. at the end, XML file saved as in specified location with given file name.</p>



<h3 class="wp-block-heading">Array to XML Complete code</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php


function generateXML($data) {
    $title = $data['class'];
    $rowCount = count($data['student']);
    
    //create the xml document
    $xmlDoc = new DOMDocument();
    
    $root = $xmlDoc->appendChild($xmlDoc->createElement("student_info"));
    $root->appendChild($xmlDoc->createElement("title",$title));
    $root->appendChild($xmlDoc->createElement("totalRows",$rowCount));
    $tabUsers = $root->appendChild($xmlDoc->createElement('rows'));
    
    foreach($data['student'] as $user){
        if(!empty($user)){
            $tabUser = $tabUsers->appendChild($xmlDoc->createElement('student'));
            foreach($user as $key=>$val){
                $tabUser->appendChild($xmlDoc->createElement($key, $val));
            }
        }
    }
    
    header("Content-Type: text/plain");
    
    //make the output pretty
    $xmlDoc->formatOutput = true;
    
    //save xml file
    $file_name = str_replace(' ', '_',$title).'.xml';
    $xmlDoc->save($file_name);
    
    //return xml file name
    return $file_name;
}


// array 
$array = array(
    'class'=> 'class 10th',
    'student'=> array(
      '0' => array(
        'name' 		=> 'David',
        'rollNo'		=> '34'
      ),
      '1' => array(
        'name' 		=> 'Sunny',
        'rollNo'		=> '30'
      ),
      '2' => array(
        'name' 		=> 'Illa',
        'rollNo'		=> '37'
      ),
      '3' => array(
        'name' 		=> 'Monna',
        'rollNo'		=> '35'
      )
    )
  );

  generateXML($array);
?></pre>



<p><strong>Output</strong>:</p>



<pre class="wp-block-code"><code>&lt;?xml version="1.0"?>
&lt;student_info>
  &lt;title>class 10th&lt;/title>
  &lt;totalRows>4&lt;/totalRows>
  &lt;rows>
    &lt;student>
      &lt;name>David&lt;/name>
      &lt;rollNo>34&lt;/rollNo>
    &lt;/student>
    &lt;student>
      &lt;name>Sunny&lt;/name>
      &lt;rollNo>30&lt;/rollNo>
    &lt;/student>
    &lt;student>
      &lt;name>Illa&lt;/name>
      &lt;rollNo>37&lt;/rollNo>
    &lt;/student>
    &lt;student>
      &lt;name>Monna&lt;/name>
      &lt;rollNo>35&lt;/rollNo>
    &lt;/student>
  &lt;/rows>
&lt;/student_info>
</code></pre>



<h2 class="wp-block-heading">Convert XML to PHP Associative Array</h2>



<p>For converting XML to PHP Associative Array we will use the few following steps.</p>



<ul class="wp-block-list"><li>We will read the XML data from the file using <strong>file_get_contents</strong>()  and convert the XML to an array using PHP.</li><li>Convert XML string into an object using <strong>simplexml_load_string</strong>() function in PHP.</li><li>Then incode the putput to JSON using <strong>json_encode</strong>() function.</li><li>Convert JSON data into array using <strong>json_decode</strong>() function.</li></ul>



<p></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

function xml_to_array(){
    //xml file path
    $path = "class_10th.xml";

    //read entire file into string
    $xmlfile = file_get_contents($path);

    //convert xml string into an object
    $xml = simplexml_load_string($xmlfile);

    //convert into json
    $json  = json_encode($xml);

    //convert into associative array
    $array_data = json_decode($json, true);
    
    print_r($array_data);
}
xml_to_array();
?></pre>



<p><strong>Output</strong>:</p>



<pre class="wp-block-code"><code>Array
(
    &#91;title] => class 10th
    &#91;totalRows] => 4
    &#91;rows] => Array
        (
            &#91;student] => Array
                (
                    &#91;0] => Array
                        (
                            &#91;name] => David
                            &#91;rollNo] => 34
                        )

                    &#91;1] => Array
                        (
                            &#91;name] => Sunny
                            &#91;rollNo] => 30
                        )

                    &#91;2] => Array
                        (
                            &#91;name] => Illa
                            &#91;rollNo] => 37
                        )

                    &#91;3] => Array
                        (
                            &#91;name] => Monna
                            &#91;rollNo] => 35
                        )

                )

        )

)</code></pre>



<h2 class="wp-block-heading">Conclusion:</h2>



<p>We have tried to explain the above code with possible example, hope you liked the article. You can also visit  <a href="https://atcodex.com/how-to/how-to-check-an-array-is-multidimensional-or-not-in-php/" target="_blank" rel="noreferrer noopener">How to check an array is multidimensional or not in PHP</a> to check if array in multidimensional or not before using it in XML conversion.</p>
<p>The post <a href="https://atcodex.com/php/array-to-xml/">Array to XML Conversion and XML to Array Convert in PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Remove multiple elements from an array in Javascript/jQuery</title>
		<link>https://atcodex.com/php/remove-multiple-elements-from-an-array-in-javascript-jquery/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Mon, 28 Sep 2020 17:31:11 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Lodash]]></category>
		<category><![CDATA[Underscore]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1045</guid>

					<description><![CDATA[<p>In this post, we will learn how to Remove multiple elements from an array using Javascript or jQuery give with an example. We can simply remove elements from an array &#8230; </p>
<p>The post <a href="https://atcodex.com/php/remove-multiple-elements-from-an-array-in-javascript-jquery/">Remove multiple elements from an array in Javascript/jQuery</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this post, we will learn how to Remove multiple elements from an array using Javascript or jQuery give with an example. We can simply remove elements from an array using javascript. Below in the post, we have tried different methods to do the same.</p>



<p>Let&#8217;s suppose we have an array in which and we want to remove some particular element from the array without refreshing the page.  In that case, we have to use javascript and jquery to remove the element from the array. Because if we go for server-side language then we have to refresh the page or need to get done the same thing with the help of AJAX and backend language. Which is a kind of long and boring task to do. We can simply use javascript, jquery, and some of the other javascript libraries to do it.</p>



<p>Following are the some example we are providing to make you  understand about the topic.</p>



<h2 class="wp-block-heading">Remove element using Vanilla JavaScript</h2>



<p>In first, let&#8217;s remove the element using simple javascript and jquery, In this, we will use Array.prototype.remove, grep() and inArray() to remove multiple as well as the duplicate value from the array in jquery. However, you can check <a href="https://atcodex.com/how-to/how-to-remove-duplicate-values-from-an-array-in-php/" target="_blank" rel="noreferrer noopener">How to remove duplicate values from an array in PHP</a></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html>
&lt;head>
   &lt;title>Jquery Remove Multiple Values from Array Example&lt;/title>
   &lt;script src="https://code.jquery.com/jquery-1.12.4.js">&lt;/script>
&lt;/head>
&lt;body>
  
&lt;script type="text/javascript">
    var sites = [ "website1.com", "website2.com", "website3.com", "website4.com" ];
  
    Array.prototype.remove = function(){
        var args = Array.apply(null, arguments); 
   
        return $.grep(this, function(value) {
           return $.inArray(value, args) &lt; 0;
        });
    }
  
    sitesNew = sites.remove("website2.com", "website3.com");
  
    console.log(sitesNew);
&lt;/script>
  
&lt;/body>
&lt;/html></pre>



<p>Output</p>



<figure class="wp-block-image size-large is-resized is-style-default"><img fetchpriority="high" decoding="async" src="https://atcodex.com/wp-content/uploads/2020/09/Untitled.png" alt="output in console" class="wp-image-1056" width="590" height="330" srcset="https://atcodex.com/wp-content/uploads/2020/09/Untitled.png 590w, https://atcodex.com/wp-content/uploads/2020/09/Untitled-300x168.png 300w" sizes="(max-width: 590px) 100vw, 590px" /></figure>



<h2 class="wp-block-heading">Remove element using Using Lodash</h2>



<p>To remove an element, we can use an external javascript library called Lodash. Lodash has the <code><a href="https://lodash.com/docs/#remove" target="_blank" rel="noreferrer noopener">remove</a>()</code> method which is very easy to use. All other methods in the Lodash are very easy to use and helpful which can done job for you in a second. You do not need to break the array  and manipulate for getting some work done in javascript. It have very helpful built in methods which you can use in any situation in the programming.</p>



<p>In the below code  we are just passing the array in _.remove() and in which we have defined callback function when passed argument is == to 3.  </p>



<p><strong>NOTE : It make the changes in the existing array value</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">var arr = [1, 2, 3, 3, 4, 5];
_.remove(arr, function(e) {
    return e === 3;
});
console.log(arr);

// Output:
// [ 1, 2, 4, 5 ]</pre>



<p></p>



<h2 class="wp-block-heading">Remove element using Using Underscore</h2>



<p>We can use Underscore library to solve the same problem. It has _.reject method which is same like _.remove() method in the Lodash. It works similarly like Lodash with some difference which is : it returns the new array after removing the passed value in the array. Old array value remains same. </p>



<p>See the following code for an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">var arr = [1, 2, 3, 3, 4, 5];
var ret = _.reject(arr, function(e) {
    return e === 3;
});
console.log(arr);
console.log(ret);

// Output:
// [ 1, 2, 3, 3, 4, 5 ]
// [ 1, 2, 4, 5 ]
</pre>



<h2 class="wp-block-heading">Conclusion</h2>



<p>We have explained 3 different method and by using them you can remove the one or more elements from the array in simple way. I hope you guys have liked the post, if you have any suggestion please comment below., i would love to help you.</p>
<p>The post <a href="https://atcodex.com/php/remove-multiple-elements-from-an-array-in-javascript-jquery/">Remove multiple elements from an array in Javascript/jQuery</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to check an array is multidimensional or not in PHP?</title>
		<link>https://atcodex.com/how-to/how-to-check-an-array-is-multidimensional-or-not-in-php/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Thu, 24 Sep 2020 12:48:23 +0000</pubDate>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1042</guid>

					<description><![CDATA[<p>Sometimes we need to check the array is multidimensional or not in the PHP. In this tutorial, we will learn different ways to check multidimensional array. In this tutorial, we &#8230; </p>
<p>The post <a href="https://atcodex.com/how-to/how-to-check-an-array-is-multidimensional-or-not-in-php/">How to check an array is multidimensional or not in PHP?</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Sometimes we need to check the array is multidimensional or not in the PHP. In this tutorial, we will learn different ways to check multidimensional array. </p>



<p>In this tutorial, we will discuss and check if array is multidimensional or not in PHP. We will look at example of PHP check array is multidimensional. We would like to share with you how to check if array is multidimensional PHP.</p>



<p>Let&#8217;s suppose we have an array in the application and we need to check that if it is multidimensional or not and based on we need to perform some activity in the application. There are few methods to check an array is multi-dimensional or not.  </p>



<p>We have <a href="https://www.php.net/manual/en/function.count.php" target="_blank" rel="noreferrer noopener">count</a>() and <a href="https://www.php.net/manual/en/function.rsort" target="_blank" rel="noreferrer noopener">rsort</a>()  function to check if the array is multidimensional or not in the PHP. count() function will produce the wrong result if the subarray is empty. So make sure while using the count() in this situation. </p>



<p>Let&#8217;s understand one by one from the below example </p>



<h2 class="wp-block-heading">Check an array is multidimensional using <strong>rsort</strong>() function</h2>



<p><strong>Syntax:</strong></p>



<pre class="wp-block-code"><code>rsort( $array )
</code></pre>



<p><strong>Parameters:</strong> The rsort() function accepts one parameter</p>



<p><strong> </strong>Let&#8217;s see how we can check the  multidimensional  array using rsort() function in the PHP.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php 
$mainArray = array( 
	
	// Default key for each will 
	// start from 0 
	array("atcodex", "to", "check"), 
	array("welcome", "World") 
); 

// Function to check array is 
// multi-dimensional or not 
function is_multi_array( $arr ) { 
	rsort( $arr ); 
	return isset( $arr[0] ) &amp;&amp; is_array( $arr[0] ); 
} 

// Display result 
var_dump( is_multi_array( $mainArray ) ); 
?> 
</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>bool(true)</code></pre>



<p></p>



<h2 class="wp-block-heading">Check an array is multidimensional using count() function</h2>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>count </strong>()</p></blockquote>



<p>Another PHP program to check an array is multidimensional or not using <strong>count </strong>function. But as i  mentioned above count() will not work if the sub array is empty.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php 
// Declare an array 
$data = array(1 => 'a', 2 => 'b', 3 => array("teach", "the", "student")); 
$gfg = array(1 => 'a', 2 => 'b'); 

// Function to check array is 
// multi-dimensional or not 
function is_multi($data) { 
	
	$rv = array_filter($data, 'is_array'); 
	
	if(count($rv)>0) return true; 
	
	return false; 
} 

// Display result 
var_dump(is_multi($data)); 
var_dump(is_multi($gfg)); 
?> 
</pre>



<p><strong>Output:</strong></p>



<pre class="wp-block-code"><code>bool(true)
bool(false)</code></pre>



<h2 class="wp-block-heading">Conclusion:</h2>



<p>You can see and understand easily that how to know if array is Multidimensional. An array containing one or more array called a multidimensional array. We are tried to explain the article with possible example hope you liked it. If you have any query related to this, please comment below, we would love to help you.</p>



<p>You can also check <a href="https://atcodex.com/php/php-multidimensional-array-search-by-key-and-value-with-examples/" target="_blank" rel="noreferrer noopener">PHP Multidimensional Array Search by Key and Value with Examples</a></p>
<p>The post <a href="https://atcodex.com/how-to/how-to-check-an-array-is-multidimensional-or-not-in-php/">How to check an array is multidimensional or not in PHP?</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PHP Multidimensional Array Search by Key and Value with Examples</title>
		<link>https://atcodex.com/php/php-multidimensional-array-search-by-key-and-value-with-examples/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Wed, 23 Sep 2020 18:37:07 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1037</guid>

					<description><![CDATA[<p>PHP search a multidimensional array (Search By key and Value). Here we will learn how to search in the multidimensional array for value and return key. Sometimes we need to &#8230; </p>
<p>The post <a href="https://atcodex.com/php/php-multidimensional-array-search-by-key-and-value-with-examples/">PHP Multidimensional Array Search by Key and Value with Examples</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>PHP search a multidimensional array (Search By key and Value). Here we will learn how to search in the multidimensional array for value and return key.</p>



<p>Sometimes we need to search in an array or multidimensional array by key or value without using any function. This tutorial shows you, the fastest way to search in a multidimensional array.</p>



<p>In this article, we would love to shows, how you can create your own function for searching Multidimensional Array. Here we will take two examples for searching in the multidimensional array using the custom created function.</p>



<p>Sometimes we need to integrate a multidimensional array search by value in php. Here you will learn php multidimensional array search by value. We are going to show you about php multidimensional array search key by value. We will look at example of how to search value in multidimensional array in php. </p>



<h2 class="wp-block-heading">What are Multidimensional Arrays</h2>



<p>An array containing one or more array called a multidimensional array. PHP supports to unlimited level deep. You can create as much as a required array inside the array in the PHP and use it in the application as per need. However, more than 3 level becomes hard to manage.</p>



<p></p>



<h2 class="wp-block-heading">PHP search a multidimensional array for value and return key</h2>



<p>If you want to search in multidimensional-array by value and return key. So you can use this below example for that:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

    $array = array(
        array(
            'id' => '100',
            'name' => 'Rock',
        ),
        array(
            'id' => '105',
            'name' => 'Test',
        ),
        array(
            'id' => '109',
            'name' => 'Michael',
        ),
        array(
            'id' => '111',
            'name' => 'Mack',
        )
    );

    function searchByValue($id, $array) {
       foreach ($array as $key => $val) {
           if ($val['id'] === $id) {
             $resultSet['name'] = $val['name'];
             $resultSet['key'] = $key;
             $resultSet['id'] = $val['id'];
             return $resultSet;
           }
       }
       return null;
    }


  $searchValue = searchByValue('105', $array);
  print_r($searchValue);die;

 ?></pre>



<p>Output</p>



<pre class="wp-block-code"><code>Array
(
    &#91;name] => Rangle
    &#91;key] => 1
    &#91;id] => 105
)</code></pre>



<h2 class="wp-block-heading">PHP search a multidimensional array for key and return value</h2>



<p>If you want to search in a multidimensional array by key and return value. So you can use the below example for that:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

    $array = array(
        array(
            'id' => '100',
            'name' => 'Rock',
        ),
        array(
            'id' => '105',
            'name' => 'Test',
        ),
        array(
            'id' => '109',
            'name' => 'Michael',
        ),
        array(
            'id' => '111',
            'name' => 'Mack',
        )
    );

    function searchByKey($keyVal, $array) {
       foreach ($array as $key => $val) {
           if ($keyVal == $key) {
             $resultSet['name'] = $val['name'];
             $resultSet['key'] = $key;
             $resultSet['id'] = $val['id'];
             return $resultSet;
           }
       }
       return null;
    }

  $searchByKey = searchByKey('2', $array);
  print_r($searchByKey);die;

 ?></pre>



<p>Output</p>



<pre class="wp-block-code"><code>Array
(
    &#91;name] => Michael
    &#91;key] => 2
    &#91;id] => 109
)</code></pre>



<h3 class="wp-block-heading" id="note">Note:</h3>



<p>It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.</p>



<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>



<p>The fastest way to search a multidimensional array. In this tutorial, you have learned how to search in a multidimensional array by key and value. Hope this tutorial helped you. If you have any queries, please mentioned that in the below comment.</p>



<p>You can also check <a href="https://atcodex.com/php/how-to-remove-null-values-from-multidimensional-array-in-php/" target="_blank" rel="noreferrer noopener">How to Remove Null Values From Multidimensional Array In PHP</a></p>
<p>The post <a href="https://atcodex.com/php/php-multidimensional-array-search-by-key-and-value-with-examples/">PHP Multidimensional Array Search by Key and Value with Examples</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to access PHP variables in JavaScript or jQuery</title>
		<link>https://atcodex.com/how-to/how-to-access-php-variables-in-javascript-or-jquery/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Sat, 19 Sep 2020 13:47:27 +0000</pubDate>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=1028</guid>

					<description><![CDATA[<p>Sometimes we need to access the PHP variables in javascript or jquery for the use in the application. In this article, let’s see how to pass data and variables from &#8230; </p>
<p>The post <a href="https://atcodex.com/how-to/how-to-access-php-variables-in-javascript-or-jquery/">How to access PHP variables in JavaScript or jQuery</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Sometimes we need to access the PHP variables in javascript or jquery for the use in the application. In this article, let’s see how to pass data and variables from PHP to JavaScript.</p>



<p>If you have a simple string value in PHP and you want to access that value in javascript or jquery then  you have two options to do so. </p>



<p>First, we can pass the data through cookies. In cookies, we can set the data in using PHP cookies and then access that cookies data in JavaScript and jquery.</p>



<p>In the second method, we can simply access the PHP value direct in the JavaScript variable. You only need to initialize the variable in javascript and access the PHP value with all open and close PHP tag inside the Single Quotation mark (&#8221;).</p>



<p>Let&#8217;s see how we can pass the PHP variable to JavaScript.</p>



<h2 class="wp-block-heading">Pass PHP variables in JavaScript or jQuery</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
    $output = 'this is dummy text';
?>
&lt;script type="text/javascript">
    var output = '&lt;?php echo $output; ?>';
    document.write(output);
    console.log(output);

&lt;/script></pre>



<p>You can simply check we just defined the value in PHP and then access that variable direct in the JavaScript.</p>



<h2 class="wp-block-heading">Pass a PHP array to a JavaScript function</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
    $output = array('this', 'text','is good');
?>
&lt;script type="text/javascript">
    var output = &lt;?php echo json_encode($output); ?>;
    document.write(output);
    console.log(output);

&lt;/script></pre>



<p>We can easily pass the PHP array to JavaScript, Only need to convert the PHP array to JSON using <strong>json_encode</strong> function and that can be accessed in the JavaScript.</p>



<h2 class="wp-block-heading">Pass Multidimensional Arrays from PHP to JavaScript</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
// PHP array
$booksData = array(
    array(
        "title" => "title1",
        "author" => "author1"
    ),
    array(
        "title" => "title2",
        "author" => "author2"
    ),
    array(
        "title" => "title3",
        "author" => "author3"
    )
);
?>

&lt;script type="text/javascript">
var booksData = &lt;?php echo json_encode($booksData) ?>;
console.log(booksData); 
&lt;/script></pre>



<h2 class="wp-block-heading">Conclusion:</h2>



<p>It is very easy to pass the data from PHP to JavaScript.  We have tried to make you understand with different case scenarios that must have helped you to understand all. </p>



<p>If you have any queries related to this post, please comment below i would love to help you.  </p>



<p>You can also check <a href="https://atcodex.com/php/how-to-get-a-random-value-from-a-php-array/" target="_blank" rel="noreferrer noopener">How to get a random key from a PHP array</a></p>
<p>The post <a href="https://atcodex.com/how-to/how-to-access-php-variables-in-javascript-or-jquery/">How to access PHP variables in JavaScript or jQuery</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
