<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="mysql-xdevapi-collectionmodify.arrayinsert" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>CollectionModify::arrayInsert</refname>
  <refpurpose>Insert element into an array field</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier> <type>mysql_xdevapi\CollectionModify</type><methodname>mysql_xdevapi\CollectionModify::arrayInsert</methodname>
   <methodparam><type>string</type><parameter>collection_field</parameter></methodparam>
   <methodparam><type>string</type><parameter>expression_or_literal</parameter></methodparam>
  </methodsynopsis>
  <para>
   Add an element to a document's field, as multiple elements of a field are represented as an array.
   Unlike arrayAppend(), arrayInsert() allows you to specify where the new element is inserted by
   defining which item it is after, whereas arrayAppend() always appends the new element at the end of the array.
  </para>

 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term><parameter>collection_field</parameter></term>
    <listitem>
     <para>
      Identify the item in the array that the new element is inserted after.
       The format of this parameter is <literal>FIELD_NAME[ INDEX ]</literal> where 
       FIELD_NAME is the name of the document field to remove the element from, 
       and INDEX is the INDEX of the element within the field.
     </para>
     <para>
      The INDEX field is zero based, so the leftmost item from the array has an index of 0.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><parameter>expression_or_literal</parameter></term>
    <listitem>
     <para>
      The new element to insert after FIELD_NAME[ INDEX ]
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   A CollectionModify object that can be used to execute the command, or to add additional operations
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><function>mysql_xdevapi\CollectionModify::arrayInsert</function> example</title>
   <programlisting role="php">
<![CDATA[
<?php
$session = mysql_xdevapi\getSession("mysqlx://user:password@localhost");
$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();

$schema     = $session->getSchema("addressbook");
$collection = $schema->createCollection("people");

$result = $collection
  ->add(
  '{"name":   "Bernie",
    "traits": ["Friend", "Brother", "Human"]}') 
  ->execute();

$collection
  ->modify("name in ('Bernie', 'Jane')")
  ->arrayInsert('traits[1]', 'Happy')
  ->execute();

$result = $collection
  ->find()
  ->execute();

print_r($result->fetchAll());
?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
Array
(
    [0] => Array
        (
            [_id] => 00005b6b5361000000000000010d
            [name] => Bernie
            [traits] => Array
                (
                    [0] => Friend
                    [1] => Happy
                    [2] => Brother
                    [3] => Human
                )
        )
)
]]>
   </screen>
  </example>
 </refsect1>

</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
